static and non-static variable in java

The variables are divided into two parts that are static and non static variable in java.  In this post, we will see the difference between static and non static variable in java and what is the use of each.
If you are a beginner or you don’t know much about the java then you must read the variable in java and static variable topic first. Because these topics are base on this post.

Static variable

A variable declares with the static keyword is known as a static variable in java. It is also known as a class variable also because it’s directly associated with the class. It is shared by all the objects of the class. A static variable doesn’t belong to an object. It means when we create an object, the static variable doesn’t store with object memory and it stores in a static pool. The static variable associated with class, that means each object shares the same variable from a single memory location. We can read it in detail.

Non – static variable

There are two types of non-static variables.
1. Local variables
2. Instance variables

1. local variables

When a variable declared inside the body of a method or block or constructor is called a local variable. You can’t access these variable outsides of the method. These variables are created in memory when the function/block/constructor is called. After completing the execution of function/block/constructor these variables are destroyed by JVM. We can access these variables only within that block because the scope of these variables exists only within the block. The initialization of the local variables is mandatory. If you don’t initialize the variable compiler will throw run time exception. You can read it in a separate post.

2. Instance Variables

A variable declared outside the method/block/constructor but inside the body is called an instance variable. It is not declared with static keyword because instance variables are non-static variables. You can read it in a separate post.

  • When an object of the class is created then the object also creates one copy of the instance variable and destroyed when the object is destroyed.
  • We can specify the access specifier for instance variables. By default, the default access specifier will be used.
  • Initialization of Instance Variable is not Mandatory. Its default value is depending on the data type of variable.
  • These variables can be accessed by creating objects.

An important point about instance variables: Every object has its own separate copy of instance variable. If we created two objects of a class, then each object contains its own instance variables.

difference between static and non static variable

1. A static variable is known as a class variable because it belongs to the class.

2. A static variable is common for each object of the class. It doesn’t create a separate copy in each object.

3. Static variable stores in the static pool. Which is shared memory for each object

4. A static member can’t declare within a method or constructor because it is a class variable that must be associated with the class.

5. All the static variable loads when JVM loads the class and destroyed when JVM unloads the class.

6. A static variable can be accessed in a static method and nonstatic method

7. A static variable can be accessed by class name.

1. A non – static variable doesn’t belong to objects. They are directly associated with objects

2. A non – static variable doesn’t belong to objects. They are directly associated with objects

3. A non-static variable doesn’t store in the static pool. They store in heap memory with objects.

4. A non-static variable can declare within a method or constructor. because it can be a local variable or instance variable.

5. The non-static variables always load in memory in memory after the static variables. Suppose a non-static variable declared in the method then must be loaded when the method will get a call. But if it is an instance variable then it will be loaded at the time when the class gets load.

6. A non-static method can’t be accessed in a static method. You can access it only in nonstatic method.

7. A non-static variable can’t be accessed by the class name. You need to create an object to access them.  

Leave a Comment