Differences Between the Instance Variables and the Static Variables

Now let us discuss the differences between the Instance variables and the Static variables:

  • Each object will have its own copy of an instance variable, whereas we can only have one copy of a static variable per class, irrespective of how many objects we create. Thus, static variables are good for memory management.
  • Changes made in an instance variable using one object will not be reflected in other objects as each object has its own copy of the instance variable. In the case of a static variable, changes will be reflected in other objects as static variables are common to all objects of a class.
  • We can access instance variables through object references, and static variables can be accessed directly using the class name.
  • Instance variables are created when an object is created with the use of the keyword ‘new’ and destroyed when the object is destroyed. Static variables are created when the program starts and destroyed when the program stops.

Syntax: Static and instance variables

class GFG
{
// Static variable
static int a;

// Instance variable
int b;
}

Java Variables

In Java, Variables are the data containers that save the data values during Java program execution. Every Variable in Java is assigned a data type that designates the type and quantity of value it can hold. A variable is a memory location name for the data.

Similar Reads

Variables in Java

Java variable is a name given to a memory location. It is the basic unit of storage in a program....

How to Declare Variables in Java?

We can declare variables in Java as pictorially depicted below as a visual aid....

How to Initialize Variables in Java?

It can be perceived with the help of 3 components that are as follows:...

Types of Variables in Java

Now let us discuss different types of variables  which are listed as follows:...

Differences Between the Instance Variables and the Static Variables

...

Conclusion

...

FAQs on Variables in Java

...

Must Read:

...