What is the difference between static and non static variables | Core Java Forum
Harika Posted on 29/06/2023

can you explain me clearly how we can differentiate static and non static variables.

Yogesh Chawla Replied on 30/06/2023

Static variables in a class are those variables which are shared by all objects of that same class.
you can also say they refer to the common property of all objects(which is not unique for each object),
for example, the company name of employees, college name of students, etc.
It makes your code memory efficient too.

For example:

class Student{
	int rollno;
	String name;
	String college="ITS";

Suppose there are 500 students in college, now all instance data members or non static data members(where static keyword is not
written in front of variable) will get memory each time when the object is created.

All students have its unique rollno and name, so instance data member is good in such case.
Here, "college" refers to the common property of all objects. If we make it static, this field will get the memory only once.

//demonstrating use of static variable

class Student{
	int rollno;//instance variable
	String name;
	static String college ="ITS";//static variable

	Student(int r, String n){
		rollno = r;
		name = n;

	//method to display the values
	void display (){
		System.out.println(rollno+" "+name+" "+college);


1. Static variables can be accessed using class name
2. Static variables can be accessed by static and non static methods
3. Static variables reduce the amount of memory used by a program
4. Static variables are shared among all instances of a class.
5. Static variable is like a global variable and is available to all methods.

1. Non static variables can be accessed using instance of a class
2. Non static variables cannot be accessed inside a static method.
3. Non static variables do not reduce the amount of memory used by a program
4. Non static variables are specific to that instance of a class.
5. Non static variable is like a local variable and they can be accessed through only instance of a class.