Scope
Navigate Classes and Objects topic: ) |
Scope
[edit | edit source]The scope of a class, a variable or a method is its visibility and its accessibility. The visibility or accessibility means that you can use the item from a given place.
Scope of method parameters
[edit | edit source]A method parameter is visible inside of the entire method but not visible outside the method.
![]() | Code listing 3.14: Scope.javapublicclassScope{publicvoidmethod1(inti){i=i++;method2();intj=i*2;}publicvoidmethod2(){intk=20;}publicstaticvoidmain(String[]args){method1(10);}} |
In code listing 3.14, i
is visible within the entire method1
method but not in the method2
and the main
methods.
Scope of local variables
[edit | edit source]A local variable is visible after its declaration until the end of the block in which the local variable has been created.
![]() | Code section 3.50: Local variables.{...// myNumber is NOT visible{// myNumber is NOT visibleintmyNumber;// myNumber is visible{...// myNumber is visible}// myNumber is visible}// myNumber is NOT visible...} |
Access modifiers
[edit | edit source]You surely would have noticed by now, the words public
, protected
and private
at the beginning of class's method declarations used in this book. These keywords are called the access modifiers in the Java language syntax, and they define the scope of a given item.
For a class
[edit | edit source]- If a class has public visibility, the class can be referenced by anywhere in the program.
- If a class has protected visibility, the class can be referenced only in the package where the class is defined.
- If a class has private visibility, (it can happen only if the class is defined nested in another class) the class can be accessed only in the outer class.
For a variable
[edit | edit source]- If a variable is defined in a public class and it has public visibility, the variable can be referenced anywhere in the application through the class it is defined in.
- If a variable has protected visibility, the variable can be referenced only in the sub-classes and in the same package through the class it is defined in.
- If a variable has package visibility, the variable can be referenced only in the same package through the class it is defined in.
- If a variable has private visibility, the variable can be accessed only in the class it is defined in.
For a method
[edit | edit source]- If a method is defined in a public class and it has public visibility, the method can be called anywhere in the application through the class it is defined in.
- If a method has protected visibility, the method can be called only in the sub-classes and in the same package through the class it is defined in.
- If a method has package visibility, the method can be called only in the same package through the class it is defined in.
- If a method has private visibility, the method can be called only in the class it is defined in.
For an interface
[edit | edit source]The interface methods and interfaces are always public
. You do not need to specify the access modifier. It will default to public
. For clarity it is considered a good practice to put the public
keyword.
The same way all member variables defined in the Interface by default will become static
final
once inherited in a class.
Summary
[edit | edit source]Class | Nested class | Method, or Member variable | Interface | Interface method signature | |
---|---|---|---|---|---|
public | visible from anywhere | same as its class | same as its class | visible from anywhere | visible from anywhere |
protected | N/A | its class and its subclass | its class and its subclass, and from its package | N/A | N/A |
package | only from its package | only from its package | only from its package | N/A | N/A |
private | N/A | only from its class | only from its class | N/A | N/A |
The cases in bold are the default.
Utility
[edit | edit source]A general guideline for visibilities is to only make a member as visible as it needs to be. Don't make a member public if it only needs to be private.
Doing so, you can rewrite a class and change all the private members without making compilation errors, even you don't know all the classes that will use your class as long as you do not change the signature of the public members.
Field encapsulation
[edit | edit source]Generally, it is best to make data private or protected. Access to the data is controlled by setter and getter methods. This lets the programmer control access to data, allowing him/her to check for and handle invalid data.
![]() | Code section 3.51: Encapsulation.privateStringname;/** * This is a getter method because it accesses data from the object. */publicStringgetName(){returnname;}/** * This is a setter method because it changes data in the object. */publicbooleansetName(StringnewName){if(newName==null){returnfalse;}else{name=newName;returntrue;}} |
In the code section 3.51, the setName()
method will only change the value of name
if the new name is not null. Because setName()
is conditionally changing name, it is wise to return a boolean to let the program know if the change was successful.
Question 3.15: Consider the following class.
![]() | Question 3.15: Question15.javapublicclassQuestion15{publicstaticfinalintQKQKQKQK_MULTIPLIER=2;publicintijijijijijijijijijAwfulName=20;privateintunununununununununCrummyName=10;privatevoidmememememememeUglyName(inti){i=i++;tltltltltltltltltlBadName();intj=i*QKQKQKQK_MULTIPLIER;}publicvoidtltltltltltltltltlBadName(){intk=ijijijijijijijijijAwfulName;}publicstaticvoidmain(String[]args){mememememememeUglyName(unununununununununCrummyName);}} |
List the fields and methods of this class that can be renamed without changing or even knowing the client classes.
unununununununununCrummyName
mememememememeUglyName()
Every field or method that is public can be directly called by a client class so this class would return a compile error if the field or the method has a new name.