1.

Difference between static and non-static variables in Java

Answer»

Any variable is a class in Java can either be STATIC or non-static. Some of the DIFFERENCES between static and non-static VARIABLES in Java is given as following:

Static Variables
Non-static variables
The static keywords are defined using the keyword static.
The non-static keywords do not contain the keyword static.
Static variables can be accessed using a class reference as there is only a SINGLE copy of them.
Non-static variables can be accessed using only an object reference.
The memory for static variables is allocated when the class is loaded.
The memory for non-static variables is allocated when an object of the class is CREATED.
Every object of the class shares the static variable i.e. it is common for every object.
All objects of the class have their own copy of a non-static variable i.e the non-static variables are specific for objects.
The memory for static variables is allocated only one time.
The memory for non-static variables is allocated whenever a new object of the class is created.
Static variables can also be called class variables as memory for static variables is allocated when the class is loaded.
Non-static variables can also be called instance variables as memory for non-static variables is allocated whenever a new instance of the class is created.


Discussion

No Comment Found