|
Answer» In the Java language, we can use two TYPES of variables that are static variable and non-static variable.
The MAIN differences between both the variables are given below:
- Static Variable Non- static Variable(instance variable)
- To specify a static variable we need to use keyword static with the variable name To specify a non-static variable we do not need to use any keyword, we can SIMPLY give a variable name
- Static variable belongs to its class, not the instance Non-static variable belongs to the instance of the class.
- Memory is ALLOCATED to the static variable, at the time of loading of the class .Memory is allocated to non-static variable whenever we create a new object.
- We can access the static variable with class reference We can access the non-static variable with an object reference
- A static variable can be shared between all the instances of class.A non-static variable can be used for a single instance.
In the Java language, we can use two types of variables that are static variable and non-static variable. The main differences between both the variables are given below:
|