Do static variables get initialized in a default constructor in java?


A static filed/variable belongs to the class and it will be loaded into the memory along with the class. You can invoke them without creating an object. (using the class name as reference). There is only one copy of the static field available throughout the class i.e. the value of the static field will be same in all objects. You can define a static field using the static keyword.

Example

public class Sample{
   static int num = 50;
   public void demo(){
      System.out.println("Value of num in the demo method "+ Sample.num);
   }
   public static void main(String args[]){
      System.out.println("Value of num in the main method "+ Sample.num);
      new Sample().demo();
   }
}

Output

Value of num in the main method 50
Value of num in the demo method 50

Initialization of static variables

If you declare a static variable in a class, if you haven’t initialized them, just like with instance variables compiler initializes these with default values in the default constructor.

Example

public class Sample{
   static int num;
   static String str;
   static float fl;
   static boolean bool;
   public static void main(String args[]){
      System.out.println(Sample.num);
      System.out.println(Sample.str);
      System.out.println(Sample.fl);
      System.out.println(Sample.bool);
   }
}

Output

0
null
0.0
false

Initialization of static variables

But if you declare an instance variable static and final Java compiler will not initialize it in the default constructor therefore, it is mandatory to initialize static and final variables. If you don’t a compile time error is generated.

Example

public class Sample{
   final static int num;
   final static String str;
   final static float fl;
   final static boolean bool;
   public static void main(String args[]){
      System.out.println(Sample.num);
      System.out.println(Sample.str);
      System.out.println(Sample.fl);
      System.out.println(Sample.bool);
   }
}

Compile time error

Sample.java:2: error: variable num not initialized in the default constructor
   final static int num;
^
Sample.java:3: error: variable str not initialized in the default constructor
   final static String str;
^
Sample.java:4: error: variable fl not initialized in the default constructor
   final static float fl;
^
Sample.java:5: error: variable bool not initialized in the default constructor
   final static boolean bool;
^
4 errors

You cannot assign values to a final variable from a constructor −

Example

public class Sample{
   final static int num;
   Sample(){
      num =100;
   }
}

Output

Sample.java:4: error: cannot assign a value to final variable num
   num =100;
^
1 error

The only way to initialize static final variables other than the declaration statement is Static block.

A static block is a block of code with a static keyword. In general, these are used to initialize the static members. JVM executes static blocks before the main method at the time of class loading.

Example

public class Sample{
   final static int num;
   final static String str;
   final static float fl;
   final static boolean bool;
   static{
      num =100;
      str= "krishna";
      fl=100.25f;
      bool =true;
   }
   public static void main(String args[]){
      System.out.println(Sample.num);
      System.out.println(Sample.str);
      System.out.println(Sample.fl);
      System.out.println(Sample.bool);
   }
}

Output

100
krishna
100.25
true
raja
Published on 05-Aug-2019 16:07:14
Advertisements