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 cannot understand the difference between the instance variable and the class variable.

Then we make you better understand with the help of the example given below:

For instance, a variable will have a separate 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 proves 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, and 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 and obj2 to test the class.

When the x value in the object is incremented then it is also seen in the obj2.

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

You can also go through a few more blog links below related to core Java:

Stack vs Heap Memory Allocation In Hindi…
Inner Class In Java In Hindi…
Instance Variable vs Class Variable In Hindi…
Difference Between Instance Variable And Static Variable In Java…
Inner Class With Suitable Example In Java…
Difference Between PHP and JAVA In Hindi…
Why Java Is A Secure Language In Hindi…
Relational operator in Hindi in Java…
Logical operator in Hindi in Java…
Assignment Operator in Hindi in Java…
Unary operator in Java example in Hindi…
Arithmetic operators in Java in Hindi…
Operators In Java In Hindi…
Types Of Inheritance In Hindi…
Garbage Collection In Java In Hindi…
Interface In Java In Hindi|java में Interface क्या होता है…
Access Specifier In Java In Hindi…
Explain Types Of Jdbc Drivers In Detail…
Super Keyword In Java In Hindi…
Static Keyword in Java in Hindi…

Conclusion:

So Friends, In this blog post(Difference Between Instance Variable And Static Variable In Java) we have seen the difference between instance variables and static variables. 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 shared by all the objects.

In the case of any queries, you can write to us at support@a5theory.com 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 feedback 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.