Hot Posts

hot/hot-posts

Core Java - Fundamentals III (Types of Variables)




  • Variables are of two types primitive (eg: int) and reference (objects).
  • Variables are also divided into 3 types, instance, static and local variables


    Instance Variables (Object Level Variables / Attributes)
  • Varies from object to object.
  • Scope is the same as object.
  • Can be declared in class, constructor or outside of any method or block.
  • Cannot be accessed from static area directly but we can access using object reference.
  • Can be accessed in instance area directly.
  • JVM provides default values, not required to initialize explicitly.


   Static Variables (Class Level Variables / Fields)
  • Doesn't vary from object to object, need to declare at class level using static modifier.
  • Single copy accessible across all objects. Scope is same as that of class.
  • Available during class loading and destroyed during unloading.
  • Static method is recommended to be called by class name instead of object reference.
  • JVM provides default values, not required to initialize explicitly.
  • Changes in values are reflected across all objects.


   Local Variables (Stack variables, Automatic/Temporary Variables)
  • Inside a block or method, destroyed once method or block is completed.
  • Need to perform initialization explicitly, else we get C.E.
  • Only Applicable modifier is final.




Post a Comment

0 Comments