What is the difference between static and non-static variables? Static Variables: - A static variable is associated with the class has only one copy per class but not for each object. An instance of a class does not have static variables.
- Static variables can be accessed by static or instance methods
- Memory is allocated when the class is loaded in context area at run time.
Non-Static Variables: - Non-static variables will have one copy each per object. Each instance of a class will have one copy of non-static variables.
- Instance variables can be accessed only by the instance methods.
- Instance variables are allocated at compile time. What is the difference between static and non-static variables? - A static variable is shared among all instances of a class.
- A non-static variable is specific to a single instance of that class.
Eg: A static variable can be shared by all users for the current running system. It’s a globally available value and can be used by all users.
|