Static and non static blank final variables in Java

JavaServer Side ProgrammingProgramming

Static variable: Declared with the help of the keyword ‘static’, they are also known as class variables. They are defined within a constructor or outside a class function. When a variable is static, it is shared between all objects of the class, irrespective of the number of objects that are created.

Demonstrating how the ‘static’ keyword, when used with variable works

Example

 Live Demo

public class Demo{
   String name;
   static String designation;
   public void display_data(){
      System.out.println("The name is: " + name);
      System.out.println("The designation of this team members is : " + designation);
   }
   public static void main(String s[]){
      Demo.designation = "Intern";
      Demo my_obj = new Demo();
      my_obj.name = "Joe";
      Demo my_obj_2 = new Demo();
      my_obj_2.name = "Joanna";
      my_obj.display_data();
      my_obj_2.display_data();
      my_obj.designation = "Senior dev";
      System.out.println("\nAfter the changes, the data is :\n");
      my_obj.display_data();
      my_obj_2.display_data();
   }
}

Output

The name is: Joe
The designation of this team members is : Intern
The name is: Joanna
The designation of this team members is : Intern
After the changes, the data is :
The name is: Joe
The designation of this team members is : Senior dev
The name is: Joanna
The designation of this team members is : Senior dev

A class named Demo contains variables and a function named ‘display_data’ that is used to display the class variables. In the main function, an instance of the class is created and a name and designation is assigned to the object variable. It is displayed and another object is created and the same is done. The data is displayed on the console. The changes reflect here.

Static final blank variable − The same definition as that of blank final variable along with the keyword ‘static’, meaning it can be initialized within a static block of code only.

Blank final variable − As the name suggests, the final variable that has no value assigned to it is known as a blank final variable. It can be initialized within a constructor only, and failing to initiate a blank final variable results in a compilation error.

Blank final variable’s working

Example

 Live Demo

public class Demo{
   private static final int val_1;
   private final int val_2;
   static{ val_1 = 1;
   }
   Demo(int val_3){
      val_2 = val_3;
   }
   public static void main(String s[]){
      Demo obj_1 = new Demo(95);
      Demo obj_2 = new Demo(99);
      System.out.println("The value of first variable is : ");
      System.out.println(Demo.val_1);
      System.out.println("The value of first variable accessed using the object : ");
      System.out.println(obj_1.val_2);
   }
}

Output

The value of first variable is :
1
The value of first variable accessed using the object :
95

A class named Demo contains variables and a constructor named ‘Demo’ that is used to assign one value to another class variable. In the main function, two instances of the class are created and their values are displayed on the console.

raja
Published on 08-Jul-2020 10:26:15
Advertisements