Scope and Visibility in Java
Objectives
- Find and declare local, instance, and class variables
- Find and declare instance and class methods
- Call instance and class methods
- Know when to use local, instance, and class variables
- Know when to use instance and class methods
Purpose of Scope
- Used correctly, variable scope enables a program to reuse and
conserve memory.
- Correct variable scope makes code more understandable and less
error prone.
- Correct method scope allows a method to be called more simply.
Variable Scope
- The scope of a variable determines what owns (contains) the
variable and how long the variable will exist (hold a value).
- Three categories of variable scope in Java: local, instance,
and class.
- The declaration of a variable determines its scope.
- A variable declaration is the variable's type and name
together. Examples:
int i;
float gpa;
Pupil p1;
String name;
double[] array;
|
Local |
Instance |
Class |
| Where to Declare |
Inside a method |
Inside a class, outside all methods, but without static |
Inside a class, outside all methods, with static |
| Owner |
Method where it is declared |
Each object (instance of the class) owns a copy |
Class where it is declared |
| Lifetime |
Only as long as the method is executing |
As long as its containing object exists |
As long as its containing class is loaded |
| Usable Where |
Only inside the method where it is declared |
In all instance methods of the class |
In all methods of the class |
Example
| Pupil |
| -schoolName : String |
| -name : String |
| -gpa : float |
|
| +toString() : String |
public class Pupil {
private static String schoolName; // class variable
private String name; // instance variable
private float gpa; // instance variable
public String toString() {
String s = "Name: " + this.name; // local variable
return s;
}
}
Method Scope
- Two categories of method scope in Java: instance and class.
- The header of a method determines its scope.
- Without static, scope is instance.
- With static, scope is class.
|
Instance |
Class |
| How to Declare |
Without static:
public returnType methodName(. . .) { |
With static:
public static returnType methodName(. . .) { |
| How to Call |
Using object name:
objectName.methodName(. . .); |
Using class name:
ClassName.methodName(. . .); |
| Variables That Can Be Used Inside |
local, instance, and class |
local and class but not instance |
this Keyword |
this keyword can be used inside an instance method |
this keyword cannot be used inside a class method |
Example
| Pupil |
| -schoolName : String |
| -name : String |
| -gpa : float |
|
| +toString() : String |
| +getSchoolName() : String |
public class Pupil {
private static String schoolName = "Western University";
private String name;
private float gpa;
public String toString() { // instance method
String s = "Name: " + this.name;
return s;
}
public static String getSchoolName() { // class method
return schoolName;
}
}
Variable and Method Scope
| Variable Scope |
Method Scope |
| Instance |
Class |
| Local |
A single local variable can be used in only one method. |
A single local variable can be used in only one method. |
| Instance |
A single instance variable can be used in all
instance methods. |
Instance variables cannot be used in class
methods. |
| Class |
A single class variable can be used in
all methods. |
Interactive Example
| Pupil |
| -schoolName : String |
| -name : String |
| -gpa : float |
|
| +toString() : String |
| +getSchoolName() : String |
Move your mouse pointer over a variable declaration, and the browser will
highlight that variable's scope.
public class Pupil {
private static String schoolName = "Western University"; // class variable
private String name; // instance variable
private float gpa; // instance variable
public Pupil(String name) { // constructor (instance method)
this.name = name;
this.gpa = 0;
}
public String toString() { // instance method
String s = "Name: " + this.name + " GPA: " + this.gpa;
return s;
}
public static String getSchoolName() { // class method
return schoolName;
}
}
Visibility
- Visibility modifiers do not change the scope (ownership or
lifetime) of a variable or method.
- They simply change where a variable or method is visible (usable
or changeable).
- Java has four visibility modifiers (least visible to most
visible):
private, (default), protected, public
|
(least visible) |
|
|
(most visible) |
|
private |
(default) |
protected |
public |
| Can be accessed from the same class |
√ |
√ |
√ |
√ |
| Can be accessed from the same package |
|
√ |
√ |
√ |
| Can be accessed from any child class |
|
|
√ |
√ |
| Can be accessed from anywhere |
|
|
|
√ |
Copyright © 2010, Maia L.L.C. All rights reserved.