Wednesday 6 June 2018

Static Variable

Cited from Java – static variable with example

"""
A static variable is common to all the instances (or objects) of the class because it is a class level variable. In other words you can say that only a single copy of static variable is created and shared among all the instances of the class. Memory allocation for such variables only happens once when the class is loaded in the memory.

Static variable can be accessed directly in a static method.

Static variable initialization
  1. Static variables are initialized when class is loaded.
  2. Static variables are initialized before any object of that class is created.
  3. Static variables are initialized before any static method of the class executes.
If a variable is 'final', the value of this variable can never be changed in the current or in any class.

Final variable always needs initialization, if you don’t initialize it would throw a compilation error.
"""

No comments:

Post a Comment