Variables in Java

Posted on

Variables in java :

 

Division one 

based on types of value represented by a variable,variables are divided into two types:

 

   Primitive variable:- can be used to represent primitive values.

                       Ex:-int x=10;

 

Reference Variables:- Can be used refer objects.

                         Ex:- student s=new student();

 

Division two

    Based on position of declaration and behavior all variables are divided into three types:-

  1. Instance variables
  2. Static variables
  3. Local variables

 

Instance variables

 

  1. If the value of a variable is varied from object to object such type of variables are called instance variables.
  2. For every object a separate copy of instance variable will be created.
  3. Instance variables should be declared within the class directly but outside of any method, block or constructor.
  4. Instance variables will be created at the time of object creation and the objects are created with new  keyword and destroyed at the time of object destruction hence the scope of instance variable is exactly same as the scope of the object.
  5. Instance variable will be stored in the heap memory as the part of the object.
  6. We can not access instance variable directly from static area but we access by using object object reference.

Eg :

class instanceDemo

{

int x=10;

public static void main(String[] args)

{

         System.out.println(x);

}

}

—————————————————————————————————————————————

Output : C.E.: non static member x cant be referenced from static area

 


 

7. We can access instance variable from instance area. Instance variables can be accessed directly by calling the               variable name inside the class. However, within static methods they should be called using the full qualified nam      name  as  ObjectReference.VariableName.

Eg :-

class instanceDemo

{

int a=10;

public static void main(String[] args)

{

instanceDemo d = new instanceDemo( );

System.out.println(d.a);

}

public void m1( )

{

System.out.println(x);

}

}

—————————————————————————————————————————————-

Output : 10

———————————————————————————————————————————- JVM always provide default values to the instance variables i.e. for numbers, the default value is 0, for Booleansit is false, and for object references it is null.Values can be assigned during the declaration or within the  constructor.

 

Eg :

class defaultValue

{

int x;

double d;

Boolean b;

String s;

public static void main (String[] args)

{

DefaultValue d1= new defaultValue();

System.out.println(d1.x); 

System.out.println(d1.d);

System.out.println(d1.b);

System.out.println(d1.s); 

}

}

————————————————————————————————————

Output : 0

                  0.0

                  False

                  Null 

————————————————————————————————————

 Instance variables are also known as object level variables or attributes.

 

Static Variables 

  • Static variable’s value is same for all the objectof the class i e if the value of the    variable does not change from object to object we have to declare such type of variables at class level by using static
  • Static variables are used to create constant value attached to the class, and final keyword can be used to  make the values constant i e to prevent it from ever being changed.

Eg : public final static int x=10;

  • In the case of static variable a separate copy will be created at class level and shared by every object of the class.
  • Static variables should be declared within the class directly but outside of any method, block or constructor.
  • Static variables are created at the time of class loading and destroyed at the time of class unloading, hence the  scope of static variable is exactly same as the scope of .class.
  • Static variables are stored in method area.
  • We can access static variables either by object reference or by class name but recommended  to use class name.
    • Within the same class it is not required to use class name and we can access directly.

Eg :

class staticDemo

{

static int x=10;

Public static void main (String[] args)

{

staticDemo d =new staticDemo ();

System.out.println(d.x); //(valid but not recommended)

System.out.println(staticDemo.x); // (valid but used in different class )

System.out.println(x); // ( valid within same class)

}

————————————————————————————————————

Output : 10

 10

 10

————————————————————————————————————

 

  • We can access static variables directly from both instance and static areas.

 

Eg :

   class staticDemo

{

Static int x = 10;

public static void main (String[] args)

{

System.out.println(x); // (valid in static area)

}

public void m1( ) 

{

System.out.println(x); // (valid in instance area )

}

}

—————————————————————————————————————————————-

Output : 10

—————————————————————————————————————————————-

 

  • JVM provides default values to the variables and we are not required to perform initialization to the variables explicitly.e. for numbers, the default value is 0, for Booleansit is false, and for object references it is null.Values can be assigned during the declaration or within the constructor.

 

Eg :

class Test

{

static int x;

static boolean b;

static String s;

public static void main( String[] args)

{

System.out.printl(x); 

 System.out.printl(b);

System.out.printl(s);

}

}

—————————————————————————————————————————————-

Output : 0

                  false

                  null

—————————————————————————————————————————————–

  • Static variables are also known as class level variables or fields.

 

Local variables

 

  • Some times to meet temporary requirements of a programmer, he can declare variable inside a method, block or constructor such type of variables are called local variables, temporary variables or automatic variables.
  • Local variables are stored in stack memory.
  • Local variables are created while executing the block in which we declare it. Once block execution completes the local variables destroyed automatically. Hence the scope of local variable is the block in which we declare it.

 

Eg :

class Demo

{

public static void main(String[] args)

{

for(int i = 0;i<5;i++)

{

System.out.println(i);

}

}

}

—————————————————————————————————————————————

  Output: 0 

                   1

                   2

                   3

                  4

 —————————————————————————————————————————————

  • The only applicable modifier for local variables is finaland if any other modifier other than final is used then it will generate compile time error saying illegal start of the expression.
  • For local variables JVM does not provide default values so the programmer has to perform the initialization of the variables explicitly before using that variable i.e if the variable is not used then it is not required to perform initialization.
  • It is highly recommended to initialize local variables at the time of declaration at least with default values.

 

Eg 1 :

class Hello

{

Public static void main(String[] args)

{

int x;

System.out.println(“hello”);

}

   }

—————————————————————————————————————————————-

Output : hello

NOTE : In this example the variable x is not used so it is not required to initialize x

—————————————————————————————————————————————-

 Eg 2 :

class Test

{

Public static void main(String[] args)

{

int x;

System.out.println(“x”);

}

   }

—————————————————————————————————————————————-

Output : compile error : variable x might not have been initialized.

 —————————————————————————————————————————————