Monday, June 1, 2020

Variable types

Variables Types:


Literal: A constant value which can be assigned to the variable is called a literal.
Int x = 10; x is name of variable, 10 is a constant value or literal
Based on value represented by a variables, all variables are divided into two types.
  1. Primitive variables: can be used to represent primitive values eg; int x = 10;
  2. Reference variable: can be used to refer objects eg; Student s = new Student();
Division two:
Based on position of declaration and behavior, variables are divided into three types:
  1. Instance variables
  2. Static variables
  3. Local variables

Instance variables:
  • If the value of a variable is varied from object to object, such type of variables are called instance variables or member variables.
  • For every object, a separate copy of instance variable will be created.
  • Access modifiers can be given to instance variables
  • The instance variables are visible for all methods, constructors and block in the class
  • Instance variable should be declared within the class directly but outside of any method or block or constructor.
  • Instance variable will be created at the time of object creation and destroyed at the time of object destruction. Hence, the scope of instance variable is exactly same as the scope of object.
  • Instance variable will be stored in the heap memory as a part of object.
  • We can not access instance variable directly from static area but we can access by using object reference
  • We can access instance variable directly from instance area
  • Instance variables have default values
        class Test {
        int x = 10;
       public static void main(String[] args) {
             Test t = new Test();
       System.out.println(t.x);
       }
}

Static Variables:
  • If the value of a variable is not varied from object to object, then it is not recommended to declare variable as static. We have to declare such type of variable at class level by using static modifier.
  • In the case of instance variables, for every object a separate copy will be created but in the case of static variables a single copy will be created at class level and shared by every object of the class
  • Static variable should be declared within the class directly but outside of any method or block or constructor.
  • Static variable will be created at the time of class loading and destroy at the time of class unloading. Hence the scope of static variable is exactly same as scope of dot class file
  • Static variable will be stored in method area
  • Static variable can be accessed either by object reference or class name but recommended one is by using class name (i.e. ClassName.VariableName) within the same class, it is not require to use class name and we can access directly. See below example.
  • We can access static variable directly from both instance and static areas
  • Static variables are declared as constants
  • Static variables are created when the program starts and destroyed when the program stops
  • Static variables are declared public since they must be available for users of the class
  • Note: calling the method is same as calling the variable. 
  • For static variables, JVM will provide default values and we are not require to perform initialization explicitly.
  • Static variables are also known as class level variables or fields
class Test {
       static int x = 10;
       public static void main(String[] args) {
             Test t = new Test();
       System.out.println(t.x);
       System.out.println(x);
       System.out.println(Test.x);
       t.m1();
       }
       void m1() {
             System.out.println();
       }
}

Local Variables:
  • Sometimes to meet temporary requirement of the programmer, we can declare variable inside a method or block or constructor, such type of variables are called local variables or temporary variables or stack variables or automatic variables.
  • Local variables will be stored inside stack memory.
  • Access modifiers can not be used for local variables
  • Local variables are visible only within the declared method, constructor or block
  • Local variable will be created while executing the block in which we declared it. Once the block execution completes, automatically local variable will be destroyed hence the scope of local variable is the block in which we declared it.
  • For local variable JVM will not provide default values and we must perform initialization explicitly before using that variable. If we are not using, then it is not require to perform initialization
  • It is not recommended to perform initialization for local variables inside logical blocks because there is no guarantee for the execution of these blocks always at runtime.
  • It is highly recommended to perform initialization for local variable at the time of declaration at least with the default values.
  • The only applicable modifier for local variable is final. By mistake if we try to apply any other modifier then, we will get compile time error.
  • If we are not declaring with any modifier, then by default it is default but this rule is applicable for instance and static variables but not for local variables.
class Test {
       void m1()
       {
             int x = 10;
             System.out.println(x);//possible
       }
       void m2() {
             System.out.println(a);//not possible
       }
}
Conclusions:
  • For instance and static variables, JVM will provide default values and we are not require perform initialization explicitly. But for local variables, JVM will not provide default values, compulsory we should perform initialization explicitly before using that variable.
  • Instance and static variables can be accessed by multiple threads simultaneously and hence these are not thread safe but in the case of local variables, for every thread, a separate copy will be created and hence local variables are thread safe. 
  • Every variable in java should be  either instance , or static or local.
  • Every variable in java should be either primitive or reference
  • Once we create an array, every array elements by default initialize with default values irrespective of whether it is instance or static or local array.

No comments:

Post a Comment