Variable in Java & its types

Definition -

Variable is a named memory location used to store data temporarily. During program execution we can modify the data.

How can a variable be created

  • Primitive Variable - These variable are created by using primitive data types.
  • Referenced Variable - These variable are created by using referenced data types.

The difference between primitive and referenced data type is that primitive data type store data directly but referenced variable hold the reference of an object.

//Example.java
class Example { 
      int x = 10;
      int y = 20;
    }
//Test.java

class Test {
      public static void main(String[] args) {
            //primitive types
           
             int a =10;
             int b= method();

             //referenced types
           
             String s1= "a";
             String s2 = new String("a");
             Example e = new Example();
          }
        static int m1() {
               return 60;
               }
          }

Limitation of Variable -

You can store single value at a time. if we assign new value, old value is replaced with new value.

Defining a Variable -

Variable creation with value is called defining a variable.

Syntax -

<Accessibility Modifier> <Modifier> <datatype> <variablename> = <value> ;

Ex -

public static int a =10;

public static Example e = new Example();

Declaring Variable -

Variable creation without value is called declaring variable.

Syntax -

<Accessibility Modifier> <Modifier> <datatype> <variablename>;

Ex -

public static int z;

public static Example e;

Types of variable

Based on class scopes, variable are divided into two types

  1. Local variable - The variable created inside a method or block are called local variable.
  2. Class level variable - The variable created at class level are called class level variable.

2 Types of class level variable

class level variable are divided into two types, based on the time they are getting memory location they are.

  1. Static variable
  2. Non-static variable

Definition -

The class level variable which has static keyword in its creation statement is called static variable else it is called non-static variable.

Memory allocation of above variable -

  • local variable get memory location when method is called and their creation statement is executed. They get memory with respect to method, so they are also called method variable. when method execution completes variable allocation will be destroyed.
  • Static variable get memory at the time of class loading into JVM. They get memory with respect to class name, so they are called class level variable.
  • Non-static variable get memory location when object is created using new keyword.

Local Variable and its rules

Rule 1. Local variable cannot be accessed from another method. Because its scope is restricted only within its method.

class Example {
      public static void main(String[] args) {
             int a =10;
             System.out.println("a" +a);
            }
      static void method() {
             System.out.println("a :" +a);  // CE : cannot find symbol 
           }
     }

Rule 2.  Local variable should not be accessed without initialization. it leads CE: "variable might not have been initialized"

class Example {
      public static void main(String[] args) {
             int a =10;
             int b;
             System.out.println("a : "+a);
             //System.out.println("b : "+b); //CE:variable b might not have been intialized.
             
             a = a+10;
           //b = b+10; //CE: variable b might not have been initialized

             b = 20;
             System.out.prinln("b : " +b);

Rule 3 - Local variable must be accessed only after its creation statement. because method execution is sequential from top to bottom. If we access it before its creation statement it leads CE : "cannot find symbol "

class Example  {
      static void method1() {
             System.out.println("a :"+a);  //wrong
             int a;
             System.out.println("a : "+a);  //wrong
             int a=10;
             System.out.println("a :"+a);  //correct
       }
       static void method2() {
              System.out.prinln("a :"+a);  //wrong
       }
]

Class Level Variables

We must create class level variable only if we want to access a value throughout the class from all its methods.

What is the output from the below program ?
class Example {
      int x=10;
    
      public static void main(String[] args) {
             System.out.println(x);
      }
}
Choose one option
//Output -

1. 10
2. CE
3. RE
4. No Output

Rule :

Non-static variable and method must be accessed with object from static methods else it leads to above CE : non-static variable cannot be referenced from static context, because non-static variable and non-static method does not get memory location directly at the time of class loading.

Below program shows creating static, non-static and local variable and accessing them from main method (static method).

class Example {
      static int a =10;
      static int b =20;
  
      int x =30;
      int y =40;

    public static void main(String[] args) {
           int p =50;
           int q =60;

           System.out.println("a :"+a);
           System.out.println("b :"+b);
          
           //System.out.prinln("x :"+x); CE
           //System.out.println("y :"+y); CE

           Example e = new Example();
           System.out.println("x :"+x);
           System.out.println("y :"+y);

           System.out.println("p : "+p);
           System.out.println("q : "+q);
      }
}





Can we declare local variable or parameter as static ?

No, local variable can't be declared as static it leads CE: illegal start of expression. because local variable get memory location only if method is called.

Final Variables

The class level or local variable that has final keyword in its definition is called final variable.

Rule - Once it is initialized by developer its value cannot be changed. if we try to change its value it leads to compile time error.

Transient variables

The class level variable that has transient keyword in its definition is called transient variable.

Rule - local variable cannot be declared as transient. it leads to CE : illegal start of expression

Volatile variable

The class level variable that has volatile keyword in its definition is called volatile variable.

Rule - local variable cannot be declared as volatile. it leads to CE: illegal start of expression

%d bloggers like this: