Open In App

Difference between static and non-static variables in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

There are three types of variables in Java:

  • Local Variables
  • Instance Variables
  • Static Variables

The Local variables and Instance variables are together called Non-Static variables. Hence it can also be said that the Java variables can be divided into 2 categories:

  • Static Variables: When a variable is declared as static, then a single copy of the variable is created and shared among all objects at a class level. Static variables are, essentially, global variables. All instances of the class share the same static variable. 
    Important points for static variables :-
    • We can create static variables at class level only. See here
    • static block and static variables are executed in the order they are present in a program.

Java




// Java program to demonstrate execution
// of static blocks and variables
class Test {
    // static variable
    static int a = m1();
   
    // static block
    static
    {
        System.out.println("Inside static block");
    }
   
    // static method
    static int m1()
    {
        System.out.println("from m1");
        return 20;
    }
   
    // static method(main !!)
    public static void main(String[] args)
    {
        System.out.println("Value of a : " + a);
        System.out.println("from main");
    }
}
//By Jatin Sharma


Output : 

14

Output : 

from m1
Inside static block
Value of a : 20
from main
  • Non-Static Variable
    • Local Variables: A variable defined within a block or method or constructor is called local variable.
      • These variables are created when the block in entered or the function is called and destroyed after exiting from the block or when the call returns from the function.
      • The scope of these variables exists only within the block in which the variable is declared. i.e. we can access this variable only within that block.
      • Initialisation of Local Variable is Mandatory.

Java




public class Student {
 
    public static void main(String[] args) {
 
        // if we try to call non-static method without using instance from static method it will get error || Non-static method 'getName()' cannot be referenced from a static context
        // getName();
        Student student = new Student();
        student.greet("Knoldus");
    }
    public void greet(String name){
        System.out.println("Hello  " + name);
    }
}
//by Jatin Sharma


Output : 

x : 1, y: 10
x : 100, y: 10
x : 1000, y: 2000

Output :

Hello  Knoldus
  • Instance Variables: Instance variables are non-static variables and are declared in a class outside any method, constructor or block.
    • As instance variables are declared in a class, these variables are created when an object of the class is created and destroyed when the object is destroyed.
    • Unlike local variables, we may use access specifiers for instance variables. If we do not specify any access specifier then the default access specifier will be used.
    • Initialisation of Instance Variable is not Mandatory. Its default value is 0
    • Instance Variable can be accessed only by creating objects.

Java




public class Studentsrecords 
    /* declaration of instance variables. */ 
    public String name; //public instance 
    String division;    //default instance 
    private int age;    //private instance 
    /* Constructor that initialize an instance variable. */ 
    public Studentsrecords(String sname) 
    
        name = sname; 
    
   
    /* Method to initialize an instance variable. */ 
    public void setDiv(String sdiv) 
    
        division = sdiv; 
    
       
    /* Method to initialize an instance variable. */ 
    public void setAge(int sage) 
    
        age = sage; 
    
   
    /* Method to display the values of instance variables. */ 
    public void printstud() 
    
        System.out.println("Student Name: " + name ); 
        System.out.println("Student Division: " + division);  
        System.out.println("Student Age: " + age); 
    
   
    /* Driver Code */ 
    public static void main(String args[]) 
    
        Studentsrecords s = new Studentsrecords("Monica"); 
        s.setAge(14); 
        s.setDiv("B"); 
        s.printstud(); 
    
//by jatin sharma


Output :

Student Name: Monica
Student Division: B
Student Age: 14

The main differences between static and non static variables are:

Static variable Non static variable
Static variables can be accessed using class name Non static variables can be accessed using instance of a class
Static variables can be accessed by static and non static methods Non static variables cannot be accessed inside a static method.
Static variables reduce the amount of memory used by a program. Non static variables do not reduce the amount of memory used by a program
In Static variable Memory is allocated only once, at the time of class loading. In non Static variable Memory is allocated each time an instance of the class is created.
Static variables Can be accessed from any part of the program. Non Static variables Can be accessed only within the class or its instance.
Static variables Exists for the entire lifetime of the program. Non Static variables Exists for the lifetime of the object.
Static variables Default value is assigned automatically. Non Static variables Default value is not assigned automatically.
Static variables are shared among all instances of a class. Non static variables are specific to that instance of a class.
Static variable is like a global variable and is available to all methods. Non static variable is like a local variable and they can be accessed through only instance of a class.


Last Updated : 11 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads