Java – Variable Scopes

The variable’s scope refers to the region where they are created and accessed in a given program or function. The variable scope also refers to its lifetime.

In this tutorial, we will learn about the scopes of the different types of Java variables.

Scope of Java Instance Variables

A variable which is declared inside a class and outside all the methods and blocks is an instance variable. The general scope of an instance variable is throughout the class except in static methods. The lifetime of an instance variable is until the object stays in memory.

Example: Scope of Java Instance Variables

In the example below, we define an instance variable puppyAge in Puppy class and using its setAge(), we’re modifying it and using getAge() method, we’re getting it. This variable is available till the lifetime of myPuppy object instance.

packagecom.tutorialspoint;publicclassPuppy{privateint puppyAge;publicvoidsetAge(int age ){// access the instance variable and modify it	   
      puppyAge = age;}publicintgetAge(){// access the instance variable  return puppyAge;}publicstaticvoidmain(String[]args){Puppy myPuppy =newPuppy();
      myPuppy.setAge(2);System.out.println("Puppy Age :"+ myPuppy.getAge());}}

Compile and run the program. This will produce the following result −

Output

Puppy Age :2

Scope of Java Local Variables

A variable which is declared inside a class, outside all the blocks and is marked static is known as a class variable. The general scope of a class variable is throughout the class and the lifetime of a class variable is until the end of the program or as long as the class is loaded in memory.

Example: Scope of Java Local Variables

In the example below, we define a class variable BREED in Puppy class. This variable is available till the lifetime of program. Being static in nature, we can access it using class name directly as shown below:

packagecom.tutorialspoint;publicclassPuppy{privateint puppyAge;publicstaticStringBREED="Bulldog";publicvoidsetAge(int age ){// access the instance variable and modify it	   
      puppyAge = age;}publicintgetAge(){// access the instance variable  return puppyAge;}publicstaticvoidmain(String[]args){Puppy myPuppy =newPuppy();
      myPuppy.setAge(2);System.out.println("Puppy Age :"+ myPuppy.getAge());// access the class variableSystem.out.println("Breed :"+Puppy.BREED);}}

Compile and run the program. This will produce the following result −

Output

Puppy Age :2
Breed :Bulldog

Scope of Java Class (Static) Variables

All other variables which are not instance and class variables are treated as local variables including the parameters in a method. Scope of a local variable is within the block in which it is declared and the lifetime of a local variable is until the control leaves the block in which it is declared.

Example: Scope of Java Class (Static) Variables

In the example below, we define two local varables in main() method of Puppy class. These variables are availble till the lifetime of method/block in which these are declared and can be accessed as shown below:

packagecom.tutorialspoint;publicclassPuppy{privateint puppyAge;publicstaticStringBREED="Bulldog";publicvoidsetAge(int age ){// access the instance variable and modify it	   
      puppyAge = age;}publicintgetAge(){// access the instance variable  return puppyAge;}publicstaticvoidmain(String[]args){Puppy myPuppy =newPuppy();
      myPuppy.setAge(2);System.out.println("Puppy Age :"+ myPuppy.getAge());// access the class variableSystem.out.println("Breed :"+Puppy.BREED);// local variablesint a =10;int b =20;int c = a + b;System.out.println("c: "+ c);}}

Compile and run the program. This will produce the following result −

Output

Puppy Age :2
Breed :Bulldog
c: 30

Important Points About Variables Scope

  • By default, a variable has default access. Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc.
  • A variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public.
  • Java provides a number of access modifiers to set access levels for classes, variables, methods, and constructors. The four access levels are −
    • default − Visible to the package. No modifiers are needed.
    • private − Visible to the class only.
    • public − Visible to the world.
    • protected − Visible to the package and all subclasses.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *