Difference Between Instance Variable And Static Variable In Java?

Hello Friends, in this blog post(Difference Between Instance Variable And Static Variable In Java) I am going to explain the difference between the instance variable and static variable or class variable.

What is the difference between the instance variable and the class variable(static variable)?|Difference Between Instance Variable And Static Variable In Java?

An instance variable is a variable whose separated copy is available to each object. A class variable is a variable whose single copy in memory is shared by all objects.

An instance variable is created in the objects on the heap memory. Class variables are stored in the method area.

instance variable example
instance variable example: Difference Between Instance Variable And Static Variable

If you could not understand the difference between instance variable and class variable then we make you better understand with the help of the example given below:

For instance, a variable will have a separated copy in each object, so if the value of the instance variable is modified in one object then it will not impact the instance variable value of another object. And below program is proving this:

//Intance variables
class test
{
//instance variable
int x = 10;
//display the variable
void display()
{
system.out.println(x);
}
}
class InstanceDemo
{
public static void main(string args[])
{
// create two references
Test obj1, obj2;
//create two objects
obj1 = new test();
obj2 = new tes();
//increment x in obj1
++obj1.x;
system.out.print("x in obj1: ");
obj1.display();
//display x in obj2
system.out.print("x in obj2: ");
obj2.display();
}
}

Output:

C:\> javac instancedemo.java
C:\> java instancedemo
x in obj1: 11
x in obj2: 10

Here we create two objects obj1, obj2 to test the class. When the x value in obj1 is incremented, it does not change the x value in object2.

Since a class variable(static variable) will have only one copy in memory and that is shared by all the objects, any modification to it will also affect other objects. This is proved in the program below:

//class variables
class test
{
//class var
int x = 10;
//display the variable
void display()
{
system.out.println(x);
}
}
class StaticDemo
{
public static void main(string args[])
{
// create two references
Test obj1, obj2;
//create two objects
obj1 = new test();
obj2 = new tes();
//increment x in obj1
++obj1.x;
system.out.print("x in obj1: ");
obj1.display();
//display x in obj2
system.out.print("x in obj2: ");
obj2.display();
}
}

Output:


C:\> javac instancedemo.java
C:\> java instancedemo
x in obj1: 11
x in obj2: 11

Here in this program, we create two objects obj1, obj2 to test the class. When the x value in the object is incremented then it is also seen in the obj2 also.

static variable
static variable example: Difference Between Instance Variable And Static Variable

Conclusion:

So Friends, In this blog post(Difference Between Instance Variable And Static Variable In Java) we have seen the difference between instance variable and static variable. in an instance variable, we share a separate copy of the variable for each object. And for the static variable, we get one single copy of the variable is shared by all the objects.

In the case of any queries, you can write to us at [email protected] we will get back to you ASAP.

Hope! you would have enjoyed this post ‘Difference Between Instance Variable And Static Variable In Java’.

Please feel free to give your important feedbacks in the comment section below.

Have a great time!

Anurag

I am a blogger by passion, a software engineer by profession, a singer by consideration and rest of things that I do is for my destination.