Class Variables And Instance Variables In Java


While methods represent the behavior of an object, variables represent the state of an object. Variables in Java are of two types – Class Variables and Instance Variables. Class variables (or static variables) are common to all instances of a class where as instance variables (or non-static variables) are specific to an object. Let’s discuss class variables and instance variables in Java and difference between them in detail.

Class Variables OR Static Variables In Java :

1) Class variables, also called as static variables, are declared with static keyword.

class StaticVariables
{
	static int i;        //Static Variable

	static String s;     //Static Variable
}

2) Class variables are common to all instances of a class i.e these variables will be shared by all objects of a class. Hence, changes made to these variables through one object will reflect in all objects.

class ClassVariables
{
	static int i = 10;        //Static Variable

	static String s = "STATIC";     //Static Variable
}

public class MainClass
{
	public static void main(String[] args)
	{
		ClassVariables obj1 = new ClassVariables();

		ClassVariables obj2 = new ClassVariables();

		//accessing class variables through obj1

		System.out.println(obj1.i);      //Output : 10

		System.out.println(obj1.s);      //Output : STATIC

		//accessing class variables through obj2

		System.out.println(obj2.i);      //Output : 10

		System.out.println(obj2.s);      //Output : STATIC

		//Making changes to class variables through obj2

		obj2.i = 20;

		obj2.s = "STATIC - STATIC";

		//accessing class variables through obj1

		System.out.println(obj1.i);      //Output : 20

		System.out.println(obj1.s);      //Output : STATIC - STATIC

		//accessing class variables through obj2

		System.out.println(obj2.i);      //Output : 20

		System.out.println(obj2.s);      //Output : STATIC - STATIC
	}
}

3) Class variables can be accessed through class name as well as object reference.

class A
{
	static int i = 100;        //Class Variable
}

public class MainClass
{
	public static void main(String[] args)
	{
		//Accessing class variable through class name

		System.out.println(A.i);

		A a = new A();

		//Accessing class variable through object reference

		System.out.println(a.i);
	}
}

Instance Variables OR Non-Static Variables In Java :

1) Instance variables, also called as non-static variables, are declared without static keyword.

class InstanceVariables
{
	int i;        //Instance Variable

	String s;     //Instance Variable
}

2) Instance variables are not common to all instances of a class. Each object will have it’s own copy of instance variables. Hence, changes made to instance variables through one object will not reflect in another object.

class InstanceVariables
{
	int i = 10;        //Instance Variable

	String s = "NON-STATIC";     //Instance Variable
}

public class MainClass
{
	public static void main(String[] args)
	{
		InstanceVariables obj1 = new InstanceVariables();

		InstanceVariables obj2 = new InstanceVariables();

		//obj1 instance variables

		System.out.println(obj1.i);       //Output : 10

		System.out.println(obj1.s);       //Output : NON-STATIC

		//obj2 instance variables

		System.out.println(obj2.i);       //Output : 10

		System.out.println(obj2.s);       //Output : NON-STATIC

		//changing obj1 instance variables

		obj1.i = 20;

		obj1.s = "INSTANCE VARIABLE";

		//obj1 instance variables

		System.out.println(obj1.i);       //Output : 20

		System.out.println(obj1.s);       //Output : INSTANCE VARIABLE

		//obj2 instance variables

		System.out.println(obj2.i);       //Output : 10

		System.out.println(obj2.s);       //Output : NON-STATIC
	}
}

3) Instance variables can be accessed only through object reference.

class A
{
	int i = 100;        //Instance Variable
}

public class MainClass
{
	public static void main(String[] args)
	{
		A a = new A();

		//Accessing instance variable through object reference

		System.out.println(a.i);

		//You can't access instance variable through class name, you will get compile time error

		//System.out.println(A.i);
	}
}

Differences Between Class Variables And Instance Variables In Java :

Class VariablesInstance Variables
Class variables are declared with static keyword.Instance variables are declared without static keyword.
Class variables are common to all instances of a class. These variables are shared between the objects of a class.Instance variables are not shared between the objects of a class. Each instance will have it’s own copy of instance variables.
As class variables are common to all objects of a class, changes made to these variables through one object will reflect in another.As each object will have its own copy of instance variables, changes made to these variables through one object will not reflect in another object.
Class variables can be accessed using either class name or object reference.Instance variables can be accessed only through object reference.

Class Variables Vs Instance Variables In Java :

class variables and instance variables in Java

Also Read :


10 Comments

  1. Thank you Pramod for this excellent job..You are the best teacher…i feel myself confident in java after learning from your site..Thank you so much

  2. Really appreciate your style of explaining the things…..many many thanks…….i was zero in java before few months and now having 4-5 offer letters …only because of you…..thanks a lot again.

  3. @Controller
    public class StController {

    @Autowired
    StService stService;

    @RequestMapping(value = “signupForm”, method = RequestMethod.GET)
    public String getData(@ModelAttribute Data data) throws Exception {
    stService.getData(data.getStud_id(), data.getName(), data.getSub(), data.getMarks());
    return “signup”;
    }
    }

Leave a Reply