In the context of class attributes, static has a different meaning. If you have a field like:

private static int sharedAttribute;

then, each and every instance of the class will share the same variable, so that if you change it in one instance, the change will reflect in all instances, created either before or after the change.

Thus said, you might understand that this is bad in many cases, because it can easiy turn into an undesired side-effect: changing object a also affects b and you might end up wondering why b changed with no apparent reasons. Anyway, there are cases where this behaviour is absolutely desirable:

  1. class constants: since they are const, having all the classes access the same value will do no harm, because no one can change that. They can save memory too, if you have a lot of instances of that class. Not sure about concurrent access, though.
  2. variables that are intended to be shared, such as reference counters &co.

static vars are instantiated before your program starts, so if you have too many of them, you could slow down startup.

A static method can only access static attributes, but think twice before trying this.

Rule of thumb: don't use static, unless it is necessary and you know what you are doing or you are declaring a class constant.

Answer from Stefano Sanfilippo on Stack Overflow
🌐
Medium
medium.com › @barbieri.santiago › understanding-static-and-instance-variables-in-java-f53173cabe8c
Understanding Static and Instance Variables in Java | by Santiago | Medium
January 26, 2025 - Instance variables belong to an individual object and are unique to each instance. Declared without the static keyword. Memory allocation occurs each time an object is created. Each object has its own copy of instance variables.
Top answer
1 of 6
36

In the context of class attributes, static has a different meaning. If you have a field like:

private static int sharedAttribute;

then, each and every instance of the class will share the same variable, so that if you change it in one instance, the change will reflect in all instances, created either before or after the change.

Thus said, you might understand that this is bad in many cases, because it can easiy turn into an undesired side-effect: changing object a also affects b and you might end up wondering why b changed with no apparent reasons. Anyway, there are cases where this behaviour is absolutely desirable:

  1. class constants: since they are const, having all the classes access the same value will do no harm, because no one can change that. They can save memory too, if you have a lot of instances of that class. Not sure about concurrent access, though.
  2. variables that are intended to be shared, such as reference counters &co.

static vars are instantiated before your program starts, so if you have too many of them, you could slow down startup.

A static method can only access static attributes, but think twice before trying this.

Rule of thumb: don't use static, unless it is necessary and you know what you are doing or you are declaring a class constant.

2 of 6
15

Say there is a test class:

class Test{
public static int a = 5;
public int b = 10;
}
// here t1 and t2 will have a separate copy of b
// while they will have same copy of a.
Test t1 = new test(); 
Test t2 = new test();

You can access a static variable with it's class Name like this

Test.a = 1//some value But you can not access instance variable like this
System.out.println(t1.a);
System.out.println(t2.a);

In both cases output will be 1 as a is share by all instances of the test class. while the instance variable will each have separate copy of b (instance variable) So

 t1.b = 15 // will not be reflected in t2.
 System.out.println(t1.b); // this will print 15
 System.out.println(t2.b); / this will still print 10; 

Hope that explains your query.

Discussions

Static and Instance Variable
Static: All objects created will share the same copy. A change made in one instance will affect all instances. Instance: Each object created will have it's own copy. A change made will affect only that instance. More on reddit.com
🌐 r/learnprogramming
3
1
October 26, 2022
What is the difference between an instance and a class (static) variable in Java - Stack Overflow
The title of this question is actually a previous examination question and I am looking for clarification / an answer to it. Please note that I am learning Java and am becoming familiar with its s... More on stackoverflow.com
🌐 stackoverflow.com
java - instance variable or static variable? - Stack Overflow
If each Text instance has its own ... instance variable. ... Sign up to request clarification or add additional context in comments. ... If s will have one and the same value throughout the application, then go ahead with static. Instead if it is really a instance varible, you may have a reference to the Text in other class and have a getter method for s in Text.java to access ... More on stackoverflow.com
🌐 stackoverflow.com
Java Static vs Instance - Stack Overflow
Generally my rule is to use instance variables if you can and only have static variables and functions if it really is generic over a class rather than an object · This is quite a good answer to a similar questions Java: when to use static methods · Rather than just linking to methods consider using the new operation to create a new object and access the method from that in ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Jtcindia
jtcindia.org › tutorials › java › Instance-Static-Variable.php
Java Instance and Static Variables - Differences and Usage
In main method we are creating ... using instance variables to store a student’s unique data. • When we declare a variable inside the class directly with a static keyword, it is called static variable....
🌐
Medium
medium.com › @noble_frost_lion_664 › static-vs-instance-variables-in-java-eb9b79f42e88
Static vs Instance Variables in Java | by Naveena Selvaraj | Medium
May 12, 2025 - Conclusion: Understanding the ... in Java. Instance variables define the unique characteristics of each object, while static variables provide a way to share state and constants across all instances of a class....
🌐
Java Guides
javaguides.net › 2023 › 11 › static-variable-vs-instance-variable-in-java.html
static variable vs instance variable in Java
March 19, 2024 - A static variable is associated with the class itself. In contrast, an instance variable is associated with a specific instance of a class.
🌐
Krivalar
krivalar.com › static-variable-vs-instance-variable
Static Variable vs Instance Variable
In Java, Static variables belong to a class and hence has the same value for all objects of the class. Instance variables belong to a specific object....
🌐
Medium
medium.com › @salvipriya97 › java-variables-static-instance-and-local-a4d4c2c5c23e
Java Variables: Static, Instance and Local | by Priya Salvi | Medium
September 22, 2023 - 3. For variables that represent properties or attributes shared by all instances (e.g., a counter for all objects of a class). ... 1. Static variables are initialized only once, when the class is loaded, and retain their values across all instances.
Find elsewhere
🌐
BeginnersBook
beginnersbook.com › 2024 › 06 › difference-between-local-instance-and-static-variables-in-java
Difference between local, instance and static variables in Java
June 2, 2024 - Instance variables are declared inside a class but outside any method, constructor, or block. ... They are accessible from any method, constructor, or block within the class (non-static context).
🌐
Medium
medium.com › @ngotantien › java-static-vs-instance-793e226a788d
Java Static vs Instance. 1. An instance variable is one per… | by Tien Tan | Medium
October 2, 2017 - Java Static vs Instance 1. An instance variable is one per Object, every object has its own copy of instance variable. Eg: public class Test{ int x = 5; } Test t1 = new Test(); Test t2 = new …
🌐
Coderanch
coderanch.com › t › 503133 › java › difference-instance-variable-static-variable
difference between an instance variable and static variable? (Beginning Java forum at Coderanch)
2. when should we define any variable as static in a class? Thanks in advance. ... Instance variables belong to an instance of the class. Static variables belong to the class itself, and may be used without instantiating the class. You should use static members when you need functionality without ...
🌐
University of Illinois
geom.uiuc.edu › ~daeron › docs › javaguide › java › anatomy › static.html
Static vs. Instance
As you can see, to refer an class's static variables and methods, you use a syntax similar to the C and C++ syntax for obtaining the elements in a structure. You join the class's name and the name of the static method or static variable together with a period ('.'). Notice that the application never instantiated the System class and that out is referred to directly from the class. This is because out is declared as a static variable--a variable associated with the class rather than with an instance of the class.
🌐
Medium
yadavrohit-dev.medium.com › static-vs-instance-variables-in-java-explained-with-a-simple-example-46460101a9e3
static vs Instance Variables in Java — Explained with a Simple Example | by Rohit Yadav | Medium
February 6, 2026 - It makes your intent clear and avoids confusion. ... If a variable represents “what an object has”, make it non-static. If it represents “what the class knows”, make it static. Once you see it this way, static stops being scary and starts making sense 🙂 · Thanks for reading! If you enjoyed this post and want to explore more of my Java insights, backend development projects, or tech experiments, feel free to check out my portfolio:
🌐
GeeksforGeeks
geeksforgeeks.org › java › difference-between-static-and-non-static-variables-in-java
Difference between static and non-static variables in Java - GeeksforGeeks
July 1, 2025 - 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.
🌐
Quora
quora.com › What-is-the-difference-between-static-and-instance-variables-and-methods-in-Java
What is the difference between 'static' and 'instance' variables and methods in Java? - Quora
Answer (1 of 3): Major difference between them is: Static variable belongs to class and has only one copy per class and it is shared by all the objects of class. A common functionally for all objects belonging to same class. Whereas instance variable is belongs to instance of class. And each in...
🌐
ScholarHat
scholarhat.com › home › tutorials › java › types of variables in jav..
Types of variables in Java with examples: Local, Instance & Static
An object of the class is then constructed to allow access to and printing of the student's name. ... A static variable in Java belongs to the class itself, not to any specific object.
Published: August 30, 2025
🌐
Quora
quora.com › What-is-the-difference-in-Java-between-an-instance-variable-and-a-static-variable
What is the difference in Java between an instance variable and a static variable? - Quora
Answer (1 of 5): VARIABLE: Variable will be of some specific datatype which contains some value that can be changed further in your program. There are mainly two types of variables: a) Instance variable b) Static variable Instance variable: the variable which is directly defined inside class ...