In Java, variables are mainly categorized into two main types based on their working and memory allocation, and these two variables are static variables and non-static variables. The main difference between them is listed below:
- Static variables: These are variables that are shared among all the instances of a class.
- Non-static variables: These are variables that belong to each individual instance of a class.
Java 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.
Note: All instances of the class share the same static variable.
Example:
// Java program to demonstrate execution
// of static blocks and variables
class Geeks {
// 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;
}
public static void main(String[] args)
{
System.out.println("Value of a : " + a);
System.out.println("from main");
}
}
Output
from m1 Inside static block Value of a : 20 from main
Java Non-static variables
Non-static variables are variables that belongs to a specified object of a class, it is also known as instance variable. These variables are declared outside of a method, constructor or block. Each object created from the class gets its own separate copy of these variables.
Note: Non-static 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.
Example:
// Java program to demonstrates
// the working of non-static variables
public class Geeks
{
// declaration of non-static variables.
public String name;
String division;
private int age;
// Constructor that initialize non-static variable.
public Geeks(String sname)
{
name = sname;
}
//Method to initialize non-static variable.
public void setDiv(String sdiv)
{
division = sdiv;
}
public void setAge(int sage)
{
age = sage;
}
// Method to display the values
public void printstud()
{
System.out.println("Student Name: " + name );
System.out.println("Student Division: " + division);
System.out.println("Student Age: " + age);
}
public static void main(String args[])
{
Geeks g = new Geeks("Monica");
g.setAge(14);
g.setDiv("B");
g.printstud();
}
}
Output
Student Name: Monica Student Division: B Student Age: 14
Static variables Vs Non-static varaibles
The main differences between static and non static variables are listed below:
| 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. |