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
- Declare private, (default), protected, and public attributes
and methods
- Know when to use private, (default), protected, and public
attributes and 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
Move your mouse pointer over a variable declaration, and the browser
will highlight that variable's
scope.
| Pupil |
|
| -
schoolName : String |
|
-name : String |
|
-gpa : float |
|
|
+Pupil(name : String) |
|
+toString() : String |
| +
getSchoolName() : String |
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;
}
}
Purpose of Visibility
- Visibility modifiers do not change the scope (ownership or
lifetime) of a variable or method.
- They simply limit 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 |
|
|
|
√ |
Interactive Example
Move your mouse pointer over a variable declaration or method header,
and the browser will highlight
the classes where that variable or method is visible.
| package p1; | |
public class C1 {
private static int a;
static int b;
protected static int c;
public static int d;
private int w;
int x;
protected int y;
public int z;
private static void m1() {...}
static void m2() {...}
protected static void m3() {...}
public static void m4() {...}
private void m5() {...}
void m6() {...}
protected void m7() {...}
public void m8() {...}
} |
public class C2 {
private C1 o = new C1();
public void m() {
// cannot access C1.a
// can access C1.b, C1.c, C1.d
// cannot access o.w
// can access o.x, o.y, o.z
// cannot call C1.m1()
// can call C1.m2(),
// C1.m3(), C1.m4()
// cannot call o.m5()
// can call o.m6(),
// o.m7(), o.m8()
}
} |
public class C3 extends C1 {
public void m() {
// cannot access a
// can access b, c, d
// cannot access w
// can access x, y, z
// cannot call m1()
// can call m2(), m2(), m4()
// cannot call m5()
// can call m6(), m7(), m8()
}
} |
|
| package p2; | |
public class C4 extends C1 {
public void m() {
// cannot access a, b
// can access c, d
// cannot access w, x
// can access y, z
// cannot call m1(), m2()
// can call m3(), m3()
// cannot call m5(), m6()
// can call m7(), m8()
}
}
|
public class C5 {
private C1 o = new C1();
public void m() {
// cannot access C1.a,
// C1.b, C1.c
// can access C1.d
// cannot access o.w,
// o.x, o.y
// can access o.z
// cannot call C1.m1(),
// C1.m2(), C1.m3()
// can call C1.m4()
// cannot call o.m5(),
// o.m6(), o.m7()
// can call o.m8()
}
} |
Copyright © 2010, Maia L.L.C. All rights reserved.