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

Purpose of Scope

Variable Scope

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
 
{class variable}
{instance variable}
{instance variable}
 
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
 
{class variable}
{instance variable}
{instance variable}
{class method}
{instance method}
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

  1. Move your mouse pointer over a variable declaration, and the browser will highlight that variable’s scope in yellow.
  2. 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
 
{class variable}
{instance variable}
{instance variable}
{class method}
{constructor (instance method)}
{instance method}
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;
    }
}