Variable and Method Scope in Java
Explains variable and method scope in the Java programming language.
Includes an interactive example of Java class, instance, and local
variables and class and instance methods.
Prerequisites
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. For example:
int i;
float gpa;
Pupil p1;
String name;
double[] array;
|
Local |
Instance (non static) |
Class (static) |
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 |
Where Usable |
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() {
// s is a local variable
String s = "Name: " + this.name;
return s;
}
}
Method Scope
|
Instance (non static) |
Class (static) |
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
|
+getSchoolName() : String
+toString() : String
|
|
|
public class Pupil {
private static String schoolName = "Western University";
private String name;
private float gpa;
public static String getSchoolName() { // class method
return schoolName;
}
public String toString() { // instance method
String s = "Name: " + this.name;
return s;
}
}
Variable and Method Scope
Variable Scope |
Method Scope |
Instance (non static) |
Class (static) |
Local |
A single local variable can be used in one
method only. |
Instance (non static) |
A single instance variable can be used in all
instance methods. |
Instance variables, including the keyword this ,
cannot be used in class methods. |
Class (static) |
A single class variable can be used in
all methods. |
Demonstration
Move your mouse pointer over a variable declaration, and
the browser will highlight that variable’s scope in
yellow.
Move your mouse pointer over a method header and your
browser will highlight in orange the
variables that method can use.
Pupil |
−schoolName : String
−name : String
−gpa : float
|
+getSchoolName() : String
+Pupil(name : String)
+toString() : String
|
|
|
public class Pupil {
// class variable
private static String schoolName = "Western University";
private String name; // instance variable
private float gpa; // instance variable
public static String getSchoolName() { // class method
return schoolName;
}
public Pupil(String name) { // constructor (instance method)
this.name = name;
this.gpa = 0;
}
public String toString() { // instance method
String s = "School Name: " + schoolName +
" Name: " + this.name + " GPA: " + this.gpa;
return s;
}
}