| Java Programming Scope | Nested Classes |
| NavigateClasses and Objects topic:() |
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.
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);}} |
Incode listing 3.14,i is visible within the entiremethod1 method but not in themethod2 and themain methods.
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...} |
You surely would have noticed by now, the wordspublic,protected andprivate at the beginning of class's method declarations used in this book. These keywords are called theaccess modifiers in the Java language syntax, and they define the scope of a given item.
The interface methods and interfaces are alwayspublic. You do not need to specify the access modifier. It will default topublic. For clarity it is considered a good practice to put thepublic keyword.
The same way all member variables defined in the Interface by default will becomestaticfinal once inherited in a class.
| 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.
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.
Generally, it is best to make data private or protected. Access to the data is controlled bysetter andgetter 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 thecode section 3.51, thesetName() method will only change the value ofname if the new name is not null. BecausesetName() 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.
unununununununununCrummyNamemememememememeUglyName()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.
| Java Programming Scope | Nested Classes |