Java-Types of Variables / Chapter - 5 /Language Fundamental


javabychetan.blogspot.com

Based on the position of declaration & behavior all variables are divided into three parts.


1.instance variable

2.static variable

                                   3.local variable


1.instance variable:-

  • If the value of a variable varied from object to object are called Instance variable.
  • For every object, a separate copy of instance variable will be created.
  • Instance variable should be declared within the class directly, but outside of any method/block/constructor.
  • Instance variable will be created at the time of object creation & destroyed at the time of object destruction. So the scope of the instance variable is exactly same as the scope of the object.
  • Instance variable will be stored in the HEAP memory as the part of an object.
  • We can't access the instance variable directly from STATIC area, but we can access it by using "object reference".
  •  We can   access the instance variable directly from instance area.         
example: 
javabychetan.blogspot.com
javabychetan.blogspot.com
                   

  • For instance variable, JVM will always provide default values.
 example:-
javabychetan.blogspot.com




2.static variable:-

  • If the value of a variable is not varied from object to object then it is not recommended to declare variable as instance variable.We have to declare that variable static variable.
  • In the case of static variable a single copy will be created and shared by every object of the class.
  • Static variable should be declare within the class directly but outside of any method/block/constructor.
  • Static variable will be stored in the method area. 
  • Static variable is created at the time of class loading & destroyed at the time of class unloading.So the scope of the static variable is exactly same as the scope of .class file.
      The following activities happen whenever we run the .class file for example   
        java Test 
  1. start JVM
  2. create & start the main thread
  3. locate the .class file
  4. load the .class file
  5. execute .class file
  6. unload .class file
  7. terminate the main thread 
  8. shutdown the JVM
  • We can access the static variable either by object reference or by class name,but it is not recommended to use class name.
  • Within the same class it is not require to use class name & we can access directly.
  • We can access the static from both instance & static area. 
example:
javabychetan.blogspot.com
javachetan.blogspot.com
  •  static variable JVM will always provide default values.
    example:-
    javabychetan.blogspot.com



3.local variable:-

  • Sometime we need a variable for temporary use.We can declare variable inside a method/block/constuctor such type of variable are called local variable.
  • Local variable will be stored in the stack memory.
  • Local variable will be created while executing the block in which we declared it.once the execution completed, automatically local variable will be destroyed.Hence the scope of local variable the block in which it declared.
example :-
javabychetan.blogspot.com
javabychetan.blogspot.com
 
  • For local variable JVM won't provide default values.If are not using then it is not required to perform initilization. 
  • If we are using compulsory we should perform initilization explicitly before using that variable.Other wise we will get compile time errors.
example:-

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com