Open In App

Difference between static and non-static variables in Java

There are three types of variables in Java:

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:






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




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




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.

Article Tags :