refresh java logo

Variable Types in Java

Java has divided variables in three different types according to their accessibility. These variables are called as :

  • Local variable
  • Instance variable
  • Static or Class variable

Local variable

Variables defined inside methods, constructors or blocks(inside curly braces{...}) are called local variables. These variables are created when execution of that method, block or constructor happens. Local variables are accessible only within the block or method in which they are created. Local variables must be initialized with some value before their use otherwise compiler throws an error. Local variables are created inside stack memory.

What if I don't initialize my local variable with some value ?

Your program won't compile. Compiler will throw compilation error hence you won't be able to run your program.

Instance variable

Variables defined inside a class but outside any method are called as instance variable. Initialization of instance variable is not compulsory, if not initialized, these variables will have default values as per their data type

Instance variables, as the name suggests belongs to the instance or object of the class. Instance variables are created only when object is created. Every object of a class has it's own copy of instance variables, changes made to the variable value of one object don't reflect in other object of that class. These variables can be called using any of the object instances as objName.instanceVarName.

Static or Class variable

Variables defined inside a class but outside any method using static keyword are known as Static variables. Initialization of class variable is also not compulsory, if not initialized, these variables will have default values as per their data type.

Class variables as the name suggests, belongs to class and is common to all objects of that class. These variables are created as soon as the class is loaded inside memory. There is only one copy of static variable that is shared with all instances of that class, if changes are made to that variable value using one instance, all other instances will also see the effect of changes. Static variable can be called using class name as className.staticVarName or using any of the object instances as objName.staticVarName.

Which approach I should prefer to access a static variable, using class name or instance ?

Though you can access static variable using instance name as well, but accessing using class name is a good programming style as static variables belongs to class not instance.

Comparison of Java variables

Features Local variable Instance variable Static variable
Declaration point In a method, constructor, or block. In a class, but outside a method. In a class, but outside a method using static keyword.
Lifetime Created when method, constructor or block is entered or executed.

Destroyed on exit.

Created when instance of class is created with new keyword.
Destroyed when object is available for garbage collection.
Created when the program starts.
Destroyed when the program stops.
Scope/Visibility Visible only in the method, constructor or block in which they are declared. Visible to all methods in the class. It could be visible to other classes as well according to it's Access Modifier. Same as instance variable
Initial value None. Must be assigned a value before it's use. Default value as per Data Type. 0 for integers, false for booleans, or null for object references etc. Same as instance variable.
Use of Access Modifier Access Modifier(public, private, protected) can not be used with local variables Can be used. Can be used
Access from outside Not possible, Local variable can be accessed within the method, constructor or block in which they are declared. Instance variables can be accessed outside depending upon it's access modifier. It can be accessed using object of that class. Same as instance variable. It can be accessed using object or class name.
Memory Area Stack memory. Heap memory Heap memory
Use Used for local computations Used when value of variable must be referenced by more than one method Generally used for constants

Java Program of Variable Types

 class Student 
   {	
      static String college = "SUMVS"; // static or class variable college
      int rollNo;  // instance variable rollNo
      String name; // instance variable name     
   
      public static void main(String [] args)
        {  
           Student s1 = new Student(); // local variable s1
           s1.rollNo = 1;
           s1.name = "Ram";
                   
           Student s2 = new Student(); // local variable s2
           s2.rollNo = 2;
           s2.name = "Shyam";          
         
           s1.printDetail();
           s2.printDetail();
         
           int count = 2; // local variable count
           System.out.println("Total students = "+ count);
        }       
      void printDetail()
        {
           int count2 = 2; // local variable count2
           System.out.println(rollNo +", "+ name+", "+ Student.college+", "+ count2);
        }
   } 

Output:

1, Ram, SUMVS, 2
2, Shyam, SUMVS, 2
Total students = 2

Memory Allocation of Java Variables

variable types and their memory

Can I declare a local variable with same name as instance or static variable name ?

Yes, you can declare a local variable with the same name as an instance or static variable name declared in a class. When accessed from within the local block or method, local variable will take precedence over others.

★★★
  • If a variable has to be used inside a single method only, it's good practice to create a local variable than instance or static variable.
  • Compiler will throw error if you initialize a local variable conditionally (e.g. inside an if without else).
  • Local variable is removed from stack memory as soon as the execution of method completed.
  • Parameters in a method acts as local variable of that method.
  • Instance variable is also called member or field variable.