English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Static file/Variables belong to the class, they will be loaded into memory along with the class. You can call them without creating an object. (Use the class name as a reference). There is only one copy of a static field available throughout the class, that is, the value of a static field is the same in all objects. You can use the static keyword to define a static field.
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 Result
Value of num in the main method 50 Value of num in the demo method 50
If you declare a static variable in a class and have not initialized them, just like using instance variables, the compiler will initialize them with default values in the default constructor.
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 Result
0 null 0.0 false
However, if you declare instance variable static, and the final Java compiler will not initialize it in the default constructor, you must initialize static and final variables. If you do not compile, an error will be generated.
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); } }
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 a value to a final variable from a constructor-
public class Sample{ final static int num; Sample(){ num =;100; } }
Output Result
Sample.java:4: error: cannot assign a value to final variable num num =;100; ^ 1 error
The only way to initialize a static final variable other than a declaration statement is through a static block.
AStatic BlockIs a block of code that uses the static keyword. Typically, these are used to initialize static members. The JVM executes the static block before the main method during class loading.
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 Result
100 krishna 100.25 true