Difference between static and non-static variables in Java
Last Updated :
01 Jul, 2025
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
// 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");
}
}
Outputfrom 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
// 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();
}
}
OutputStudent 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. |
Similar Reads
Difference between static and non-static method in Java A static method is a method that belongs to a class, but it does not belong to an instance of that class and this method can be called without the instance or object of that class. Every method in java defaults to a non-static method without static keyword preceding it. Non-static methods can access
6 min read
Static and Non Static Blank Final Variables in Java In Java, a variable provides us with named storage that our programs can manipulate. In Java, a blank final variable is a final variable that is declared but not immediately initialized. It must be assigned exactly once before use. When a variable is declared with static, it becomes a static blank f
6 min read
Difference Between Static and Non Static Nested Class in Java Nested classes are divided into two categories namely static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes. A class can either be static or non-static in java. So there is a lot of difference between makin
4 min read
Difference between volatile and transient keywords in Java Just like any other programming language, Java has a set of keywords which are reserved and have a special meaning. In this article, we will see the difference between the keywords volatile and transient. Before getting into the differences, let us first understand what each of them actually means.
3 min read
Difference between Instance Variable and Class Variable Instance Variable: It is basically a class variable without a static modifier and is usually shared by all class instances. Across different objects, these variables can have different values. They are tied to a particular object instance of the class, therefore, the contents of an instance variable
2 min read
Difference Between Static and Const in JavaScript Static variable: A static variable in JavaScript is basically a property of the class which is not used on the object of the class but is used in the class itself. This static variable is stored into the data segment of the memory and its value is shared among all the objects/instances created in th
3 min read