Difference between static and non static variables in Java
Hello coders, in this tutorial we will learn the basic types of variables in Java and differentiate between static and non-static variables in Java.
A variable is used to store information that can be used in Java program to perform any task. There are three types of variables in java, and they are as follows:
1. Local variable
2. Instance variable
3. Static variable
1. Local variable: A local variable is a variable that is local to a specific method. In other words, the variable can be used only in the method of the class where it is defined.
2. Instance variable: The variable that is declared in the class and can be used by any method of that class only is called an instance variable. It is instance-specific, which means other classes are not aware of its existence.
3. Static variable: The variable which is declared using the keyword static and shared among all the classes is called a static variable.
The local variable and the instance variable are non-static variables.
Example of the above-explained types of variables in java:
class Class1{ int a;//Instance Variable public static int b=20;//Static variable } public class Demo{ public void demo1() { int c=15,res;//Local Variable res=c*c; System.out.println("Square of Local variable "+ c + " : "+res); } public static void main(String []args){ Demo obj=new Demo(); obj.demo1(); Class1 ob1=new Class1(); ob1.a=35; System.out.println("Square of Instance variable "+ ob1.a +" : " + ob1.a*ob1.a); System.out.println("Square of Static variable "+ Class1.b +" : "+ Class1.b*Class1.b); } }
Output for the above code is:
Square of Local variable 15 : 225 Square of Instance variable 35 : 1225 Square of Static variable 20 : 400
In the above code, it is shown the definition and calling the local, instance and static variables in a java program.
In addition, to learn about how to call a non-static function from a static function click here.
The difference between Static and non-static variables in java :
Static variable
- A static variable is declared using the static keyword.
- Memory is allocated at the run-time when the class is loaded and only once. However, final keyword can be used to show that the value of a static variable remains the same after initialization.
- The static as well as non-static methods can access a static variable.
- A static variable is common to every object of the class.
- It is like a global variable, available to all.
- Above all, the class name is used to access the static variable outside the class.
Non-Static variable
- A non-static variable is declared as a regular variable.
- Memory is allocated when a new object is created. However, each time a new object is created new memory space is allotted which can lead to wastage of memory.
- The static methods cannot use a non-static variable.
- A non-static variable is accessible to its instance(where it is defined).
- It is a local variable and available to object of the class.
- Accessing a non-static variable is done using the reference to the instance where it is declared.
In conclusion, the article describes the difference between static and non-static variables.
Leave a Reply