Java Variable Scopes - Understanding Instance, Local, and Static Scopes
Learn about Java variable scopes including instance, local, and static variables. This tutorial explains the region and lifetime of each variable type, helping developers understand how and where variables can be accessed and their memory lifecycle in Java programs.
Java - Variable Scopes
The scope of a variable refers to the region where it is created and accessed within a program or function. Additionally, a variable's scope also refers to its lifetime, which determines how long it remains in memory. In this tutorial, we will learn about the scopes of different types of Java variables.
Scope of Java Instance Variables
Instance variables are declared inside a class but outside all methods and blocks. They are available throughout the class except within static methods. The lifetime of an instance variable is as long as the object exists in memory.
Example: Scope of Java Instance Variables
package com.tutorialsarena;
public class Puppy {
private int puppyAge;
public void setAge(int age) {
// access the instance variable and modify it
puppyAge = age;
}
public int getAge() {
// access the instance variable
return puppyAge;
}
public static void main(String []args) {
Puppy myPuppy = new Puppy();
myPuppy.setAge(3);
System.out.println("Puppy Age: " + myPuppy.getAge());
}
}
Output
Puppy Age: 3
Scope of Java Class (Static) Variables
Class variables are declared as static inside a class. These variables are available throughout the class and last until the program ends or the class is unloaded from memory. Being static, they can be accessed directly using the class name.
Example: Scope of Java Class Variables
package com.tutorialsarena;
public class Puppy {
private int puppyAge;
public static String BREED = "Golden Retriever";
public void setAge(int age) {
// access the instance variable and modify it
puppyAge = age;
}
public int getAge() {
// access the instance variable
return puppyAge;
}
public static void main(String []args) {
Puppy myPuppy = new Puppy();
myPuppy.setAge(4);
System.out.println("Puppy Age: " + myPuppy.getAge());
// access the class variable
System.out.println("Breed: " + Puppy.BREED);
}
}
Output
Puppy Age: 4
Breed: Golden Retriever
Scope of Java Local Variables
Local variables are declared inside a method or block. They exist only within that block and are destroyed once the block is exited. This also applies to method parameters.
Example: Scope of Java Local Variables
package com.tutorialsarena;
public class Puppy {
private int puppyAge;
public static String BREED = "Labrador";
public void setAge(int age) {
// access the instance variable and modify it
puppyAge = age;
}
public int getAge() {
// access the instance variable
return puppyAge;
}
public static void main(String []args) {
Puppy myPuppy = new Puppy();
myPuppy.setAge(5);
System.out.println("Puppy Age: " + myPuppy.getAge());
// access the class variable
System.out.println("Breed: " + Puppy.BREED);
// local variables
int a = 15;
int b = 25;
int c = a + b;
System.out.println("c: " + c);
}
}
Output
Puppy Age: 5
Breed: Labrador
c: 40
Important Points About Variable Scope
- By default, a variable has default access, meaning it is visible to other classes in the same package.
- A variable or method without any access control modifier is available to any other class in the same package.
- Fields in an interface are implicitly public, static, and final. Methods in an interface are by default public.
- Java offers four access levels: default, private, public, and protected.
Access Levels
- default: Visible to the package. No modifiers needed.
- private: Visible to the class only.
- public: Visible to the world.
- protected: Visible to the package and all subclasses.