Scope in Java

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
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

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
+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 (non static) Class (static)
Local A single local variable can be used in only one method. A single local variable can be used in only one method.
Instance
(non static)
A single instance variable can be used in all instance methods. Instance variables cannot be used in class methods.
Class
(static)
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;
	}
}

Copyright © 2010, Maia L.L.C.  All rights reserved.