Wednesday, March 29, 2017

Types of Variables in java Java High Q

Types of Variables:-
Based on type of values represent by a variable all variables ate divided into 2 types
Primitive variables:-
                Can be used to hold primitive values
                Ex: - int x=20;
Reference variables:-
                Can be used to refer objects
                                Ex:- String s=new String();
                                S----> reference
Division II:-
Based on purpose and position of declaration all variables are dived into 3 types
1.       Instance variable
2.       Static variable
3.       Local variable
4.       Global variable

i.                     Instance variable


è If the value of a variable is varied from object to object such type of vaiables are called instance variables
è For every object a separate copy of instance variable will be created
è Instance variable will be created at the time of object creation and instance variable will be destroying at the time of object destruction.
è  Hence the scope of instance variable is exactly same as the scope of object.
è Instance variables will be stared or saved in the heap area as the part of object.
è Instance variables should be declared with in the class directly but outside of any method
of block of constructor
è We can’t access instance variables directly from static area but we can access by suing object reference.
è We can access instance variables directly from instance area.



Eg:- package com.javahighq.test;

public class Test {
      int x=10;
public static void main(String[] args) {
System.out.println(x);//Cannot make a static reference to the non-static field x
//compile time exception non-static variables x can not be refered from a static context
Test t=new Test();
System.out.println(t.x);
}
public void m1(){
System.out.println(x);
}
}



 è For instance variable JVM will always provide default values and we are not required to perform initialization explicitly.


Ex:- package com.javahighq.test;

public class Test {
double d;
String s;
boolean b;
int i;
public static void main(String[] args) {
      Test jt=new Test();
      System.out.println(jt.d);//0.0
      System.out.println(jt.s);//null
      System.out.println(jt.b);//false
      System.out.println(jt.i);//0
}
}

 è Instance variable also throws as object level variables or attributes

class Student {
String name;
int rollno;
.
.
.
ms()                         //method
student(){               // constructor
}


Static variables :-

è If the value of a variable is not varied from object to object then it is not recommended to declare that variable as instance variable we have to declare such type of variable at class level with static modifier.
è In the case of instance variable for every object a separate copy will be crated but in the class directly but outside of any method or block or constructor.
è Static variables will be created at the time of class coding and destroyed at the time of class unloading hence the scope of static variable is exactly same as scope of .class file.
è Static variables should be declared with in the class directly but outsider of any method or block or constructor.
è Static variables will be created at the time of class loading and destroy at the time of class unloading hence the scope of static variables is exactly same as scope of .class file
è Static variables will be stored in method area.
è We can access static variables either by using object reference or by using class name but recommended to use class name with in the same class even class name not required we can access directly.






Ex:-  package com.javahighq.test;

public class Test {
static int x=30;
public static void main(String[] args) {
      Test jt=new Test();
      System.out.println(jt.x);//30
      System.out.println(Test.x);//30
      System.out.println(x);//30 
}
}

We can access static variables directly from both instance and static areas
Ex:-   package com.javahighq.test;

public class Test {
static int x=30;
public static void main(String[] args) {
      Test jt=new Test();
      System.out.println(x);//30 
      jt.m1();//30
}
void m1(){
      System.out.println(x);
}
}

For static variables we are not required to perform initialization explicitly JVM will always provide default values

Ex:-   package com.javahighq.test;

public class Test {
static int x;
static String s;
static double d;
public static void main(String[] args) {
      System.out.println(x);//0
      System.out.println(s);//null
      System.out.println(d);//0.0
}
}

Static variables also known  as class level varialbels or fields



Ex:-   package com.javahighq.test;


public class Test {
int x=30;
static int y=20;
public static void main(String[] args) {
      Test jt=new Test();
      jt.x=20;
      jt.y=56;
      Test jt1=new Test();
      System.out.println(jt1.x+"----------"+jt1.y);//30----------56
}
}





 Local variables :-

è Some times to meet temporary requirements of the programmer we can declare variables insider a method or block  or constructor  such type of variables are called local variables or static  variables or temporary variables or automatic variables.
è Local variables will be stored inside stack memory
è Local variables will be created while execution their block in which we declared it. Once block execution completes automatically local variables will be destroys hence the scope of local variables is the block in which we destroyed it.



Ex:-
package com.javahighq.test;


public class Test {
public static void main(String[] args) {
int i=30;

for (int j=0;j<10;j++){
      i=i+j;
}
System.out.println(i+"------------"+j);
//j cannot be resolved to a variable because J is local variable
}
}


For local variables JVM won’t provide default values compulsory we have to perform initialization explicitly before using that variable i.e if we are not using a variable then it is not required to perform initialization explicitly .


Ex:-
package com.javahighq.test;


public class Test {
public static void main(String[] args) {
      int x;
      System.out.println(x);
      //The local variable x may not have been initialized
}
}



package com.javahighq.test;


public class Test {
public static void main(String[] args) {
      int x;
      if(args.length>0){
            x=10;
      }
      System.out.println(x);
      //The local variable x may not have been initialized
}
}

package com.javahighq.test;


public class Test {
public static void main(String[] args) {
      int x;
      if(args.length>0){
            x=10;
      }
      else {
            x=20;
      }
      System.out.println(x);//20
}
}

Note:-
 If is not recommended to perform initialization for local variables insider local blocks because there is no guaranteed for the execution of these blocks at runtime.
It is highly recommended to perform initialization for local variables at the time of declaration at least with default values int x=30;
The only applicable modifier for local variables is final by mistake if we are trying to apply any other modifier then we will get compile time error.


Ex:-

package com.javahighq.test;


public class Test {
public static void main(String[] args) {
      public int x=10;//illegal state of exception
      private int y=10; //illegal state of exception
      protected int m=10; //illegal state of exception
      static int i=10; //illegal state of exception
      transient int d=10; //illegal state of exception
      volatile int r=10; //illegal state of exception
      final int g=10//illegal state of exception
}
}


Note :-
If we are not declaring any access modifier then by default  it is default but this role is applicable only for instance & static variables but not for local variables.

Summary:-

For instance and static variables JVM will always provide default values and we are not required to perform initialization explicitly before using that variable. 

No comments:

Post a Comment