Variables

Variables

                                                       
  Variables

Variables in Java are classified into primitive and reference variables. From the programmer's perspective, a primitive variable's information is stored as the value of that variable, whereas a reference variable holds a reference to information related to that variable. reference variables are practically always objected in Java. Let's take a look at both of these types with the help of two examples.
 
Tow type of variable.

1-: primitive variable
/*
int x=10; 
byte b=20;
short a=30;
char c='a';
*/  
Can be used to represent primitive values in above the example
2-: reference variable
Can be used to refer objects example-:
/*
Student s=new Student();
*/
Based on position of declaration and behaviour all variable divide into three types
a-: Instance
b-:Static
c-:Local variables
                                            Instance variable
If the value of variable varied from object to object that’s call instance variable. Instance variable should be declare within the class directly but out side of any method ,block or constructor.
example-:
/*
Class Test{

s is object type variable refer to s

                String name;

                Int x=20;

}is valid

*/

/*

class Test{

    Test(){

              String a;//not valid it is local variable

               Int x;//not valid it is local variable

         }

    m(){

                String s;//not valid it is local variable

                Int x=20;//not valid it is local variable

           }

}

*/

case 1-:For every object a separate copy of instance variable will be created.
case 2-:Instance variables will be created at the object creation and destroyed at the time of object distraction hence the scope of instance variable is same as the scope of object.

case 3-:We can’t access instance variables directly from a static area .But we can access them by using object reference .But we can access instance variables directly from the instance area .


Example-:
/*

Class Test{

int x=20;

Public static void main(String[] args){

    System.out.print(x);// compile time error                                    

   Test t=new Test();

System.out.print(t.x);//is valid print 20

}

Public void m(){

System.out.print(x);//is valid 20

}

}

*/

case 4-:For instance variable jvm will provide default values and we are not required to perform initialization explicitly.
Example-:
/*
  class Test{

int x;

double d;

boolean b;

String s;

Public static void main(String[] args){

Test t=new Test();

     System.out.print(t.x);//0

     System.out.print(t.d);//0.0

     System.out.print(t.b);//false 

    System.out.print(t.s);// null

}

*/

Note-:Instance variable will be stored in the heap memory as a part of object.

Note-:Instance variable known as object level variable or attributes.

                                                        Static Variable 


We have to declare a set class level by using a static modifier.

In the case of instance variables for every object a separate copy will be created but in the case of a static variable a single copy will be created and shared by every object of class.

Static variables should be declared within the class directly but outside of any method or constructor.
  • Static variables will be created at the time of class loading and destroy class unloading hence scope of static variable is exactly same as class file.
  • We can access static variables either by object reference or by class name but recommended to use class name.
  • Within the same class it’s not required use class name and we can access it directly .
  • We can access variables directly from both instance and static areas.
  • For static variables jvm will provide default values and we are not required to perform initialization explicitly.
  • Static variable also known as class level variable or fields
   Example-:
   /*
//how to access static variable.

class Test{

static int x=10;

public static void main(String[] argos){

        Test t=new Test();

        System.out.print(t.x);

        System.out.print(Test x);

        System.out.print(x);

}

public void m(){

    System.out.print(x);

}

}

*/

/*

//jvm provide default value for static variable

class Test{

          static int x;

           static double y;

          static String x;

public static void main(String[] argos){

    System.out.print(x);//0

    System.out.print(y);//0.0

    System.out.print(s);//null

}

}

*/


show all process in the below image when loading static variable and when destroying static variables.

Instance variable                        

  • Instance variable are also known as object level variables or attributes.
  • Instance variable create a copy for every object.
Static Variable
  • Static variable are also known as class level variable or fields.
  • Static variable create a single copy For every object.


                                                                                    Local variable
Local variable -: Some time to meet temporary requirements of the programmer we can declare variables inside a method or block or constructor such that type variables are called local variables are temporary variables or stack variables are automatic variables.







    Note-:Local variables will be stored inside stack memory .


  • When the local variables will be created while executing the block ,in which be declared it.
  • Once block execution completes automatically the local variable is destroyed hence the scope of local variable is the block in which we declared it.
  • Local variable also called thread safe.
  • For local variable jvm won’t provide default value compulsory we should initialization explicitly.






  • The scope of local





Example-: 

/*

   Class Test{

            Public static void main(String[] args){

Int x;

System.out.print(x);

    }

} // error-: variable x might not have been initialized.

I got error variable not initialized in the scope.

*/


case 1-:Before using variables  it is required to perform initialization.

/*

Class Test{

            Public static void main(String[] args){

Int x;

System.out.print(“Hello”);

    }

*/


/*

Class Test{

            Public static void main(String[] args){

Int x;

if(args.length>0){

  x=20;

}

System.out.print(x);

    }

*//I got compile time error -: variable x might not have been initialized y




case 2-:The only applicable modifier for local variable is final by mistake try to apply any other modifier then we will compile time error.

Example-: 

/*

Class Test{

     Public static void main(String[] argos){

public int x=20;

private int x=20;

  protected int x=20;

static int x=20;

translate int x=20;

volatile int x=20;

// above all are invalid compile time error-: illegal start of expression //

Final int x=20;//valid

int x=20;//valid

}

}

*/



case 3-:If we are not declaring with any modifier then by default it is default but this rule is applicable only for instance and static variables but not for local variables .

Conclusion



For instance and static variables jvm will provide default values and we are not required perform initialize explicitly

But for local variables jvm won’t provide default value compulsory we should 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 case of local variables for every thread separate copy will be created and hence local variable are thread safe.





Every variable in java should be either instance or static or local

Every variable in java should be either primitive or referenced.

Hence various possible combinations of variables in java are .




Undeclare array

/*

Class Test{

   Int[] x;

   int [] y=new int[2];

   Static int[] s;

   Static int[] ss=new int[3];

   Public static void main(String[] args){

    Test t=new Test();

    System.out.print(t.x);//null

System.out.print(t.x[0]);//run time exception null pointer exception//

System.out.print(t.y);//[I@8sddf

System.out.print(t.y[0]);//0

system.out.print(s)// null;

system.out.print(s[0])////run time exception null pointer exception//

system.out.print(ss)//[I@9dfdddfd

system.out.print(ss[0])//0

}

}

*/


Previous Post Next Post

Most Recent