Variables

Variables

Variable is a data name that can be used to store a data value. In Java, variables are used to store data that can be manipulated or accessed within a program.

Variables

Case 1: Based on type of value represented by variable all variables are divided into 2 types

  1. Primitive Variables
  2. Reference Variables
1. Primitive Variables

Primitive Variables can be used to hold primitive values.

E.g

int x = 21;
2. Reference Variables

Reference Variables can be used refer objects.

E.g

Employee e = new Employee();

Case 2: Based on purpose and position of declaration all variables are divided into 3 types.

  1. Instance Variables
  2. Static Variables    
  3. Local Variables
1. Instance Variables
  • If the value of the variable is varied from object to object such type of variables 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 variables will destroy at the time of object destruction. Hence the scope of instance variable is exactly same as the scope of the object.
  • Instance variables will be stored in the heap area as the part of the Object.
  • Instance variables should be declared within the class directly but outside of any method or block or constructor.
  • We can’t access instance variables directly from static area but we can access by using object reference.
  • Instance variables also known as object level variables
  • We can access instance variables directly from instance area.
package com.ashok.test;

public class Test{
   int x = 10;
   public static void main(String args[]) {
      System.out.println(x); // C.E: Non static variable x can't be referenced from a static context
      Test t = new Test();
      System.out.println(t.x); //10
   }
   public void m1() {
      System.out.println(x); //10
   }
}
  • For instance variables JVM will always provide default values and we are not required to perform initialization explicitly.
package com.ashok.test;

public class Test {
   int i;
   boolean b;
   double d;
   String s;
   public static void main(String args[]) {
      System.out.println(i); // C.E: Non static variable x can't be referenced from a static context
      Test t = new Test();
      System.out.println(t.i); //0
      System.out.println(t.d); //0.0
      System.out.println(t.b); //false
      System.out.println(t.s); //null
   }
}
2. Static Variables
  • If the value of the 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 variables at class level with static modifier.
  • In the case of instance variable 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 that class.
  • Static variables should be declared with in the class directly or outside of any method or block or constructor.
  • Static variables will be created at the time of class loading and destroyed at the time of class loading hence the scope of the static variable is exactly same as scope of .class file.

E.g: Java Test

  1. Start JVM
  2. Create and start main thread
  3. Locate Test.class
  4. Load Test.class
  5. Execute main method
  6. Unload Test.class
  7. Terminate main thread
  8. Shut down JVM
  • 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.
package com.ashok.test;

public class Test{
   static int i = 10;
   public static void main(String args[]) {
      Test t = new Test();
      System.out.println(t.i); //10
      System.out.println(Test.i); //10
      System.out.println(i); //10
   }
}
  • We can access static variables directly from both instance and static areas
package com.ashok.test;

public class Test{
   static int i = 10;
   public static void main(String args[]) {
      System.out.println(i); //10
      public void m1() {
         System.out.println(i); //10
      }
   }
}
  • For static variables JVM will always provide default values and we are not required to perform initialization explicitly.
package com.ashok.test;

public class Test{
   static int i;
   static boolean b;
   static double d;
   static String s;
   public static void main(String args[]) {
      System.out.println(i); //0
      System.out.println(d); //0.0
      System.out.println(b); //false
      System.out.println(s); //null
   }
}
  • Static variables also known as class level variables or fields.
package com.ashok.test;

public class Test{
   static int i = 10;
   int j = 20;
   public static void main(String args[]) {
      Test t1 = new Test();
      t1.i = 111;
      t1.j = 222;
      Test t2 = new Test();
      System.out.println(t2.i); //111
      System.out.println(t2.j); //20
   }
}
3. Local Variables
  • Some times to meet temporary requirements of the programmer we can declare variables inside a method or block or constructor such type of variables are called local variables or stack variables or temporary variables or automatic variables.
  • Local variables are stored inside stack memory.
  • Local variables are created while executing the block in which we declared it.
  • Once block execution completes automatically local variables will be destroyed. Hence the scope of the local variables is the block in which we declared it.
package com.ashok.test;

public class Test{
   public static void main(String args[]) {
      int i = 10;
      for(int j=0;j<3;j++)
         i = i + j;
      }
      System.out.println(j); //C.E: Can't find symbol symbol: variable j location: class Test
   }
}

For local variables JVM won’t provide default values compulsory we have to perform initialization explicitly before using that variable otherwise we will get compilation error. If the variable is not used then it is not required to perform initialization explicitly.

package com.ashok.test;

public class Test{
   public static void main(String args[]) {
      int i;
      System.out.println("Hai this is Ashok"); // Fine
   }
}
package com.ashok.test;

public class Test{
   public static void main(String args[]) {
      int i;
      System.out.println(i); // C.E: Variable i might not have been initialized
   }
}
package com.ashok.test;

public class Test{
   public static void main(String args[]) {
      int i;
      if(args.length > 0) {
         i = 20;
      }
      System.out.println(i); // C.E: Variable i might not have been initialized
   }
}

Note

  1. It is not recommended to perform initialization for local variables inside a logical blocks because there is no guarantee for the execution of these blocks at runtime.
  2. It is highly recommended to perform initializations for local variables at the time of declaration at least with default values.
  3. The only applicable modifier for local variable is final. By mistake if you are trying to apply any other modifier then we will get compile time error.
package com.ashok.test;

public class Test{
   public static void main(String args[]) {
      public int a = 10; // C.E: Illegal start of expression
      private int a = 10; // C.E: Illegal start of expression
      protected int a = 10; // C.E: Illegal start of expression
      static int a = 10; // C.E: Illegal start of expression
      volatile int a = 10; // C.E: Illegal start of expression
      final int a = 10; // Fine
   }
}

That’s all about Variables in Java. If you have any queries or feedback, please write us at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!

Variables
Scroll to top