English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Static variables-Static variables are also known as class variables. You can declare a static variable using a keyword. Once a static variable is declared, there is only one copy in the class, regardless of how many objects are created in the class.
public static int num = 39;
Instance variables-These variables belong to the instance of the class (object). These are declared within the class but outside of methods. These are initialized when the class is instantiated. They can be accessed from any method, constructor, or block of the specific class.
You must use an object to access instance variables. That is, to access instance variables, you need to create an object of the class, and using the object, you need to access these variables.
final-After declaring a variable final, you cannot reassign it.
Uninitialized final variables are called blank final variables. Like instance variables, final variables will not be initialized with default values. Therefore,You must initialize the final variables after declaring them..
However, if you try to use an empty variable in the code, it will generate a compile-time error.
In the following Java program, the Student class contains two final variables, name and age, which are not initialized.
public class Student { public final String name; public final int age; public void display(){ System.out.println("Name of the Student: "+this.name); System.out.println("Age of the Student: ")+this.age); } public static void main(String args[]) { new Student().display(); } }
When compiling, the program will generate the following errors.
Student.java:3: error: variable name not initialized in the default constructor private final String name; ^ Student.java:4: error: variable age not initialized in the default constructor private final int age; ^ 2 errors
To solve this problem, you need to initialize the declared final variables.-
public class Student { public final String name; public final int age; public Student(){ this.name = "Raju"; this.age = 20; } public void display(){ System.out.println("Name of the Student: "+this.name ); System.out.println("Age of the Student: ")+this.age ); } public static void main(String args[]) { new Student().display(); } }
Output Result
Name of the Student: Raju Age of the Student: 20
In the same way, if you declare a static variable final without initializing it, it is treated as a static final variable.
When a variable is declared as both a static variable and a final variable, you can only initialize it in the static block. If you try to initialize it elsewhere, the compiler assumes that you are trying to reassign a value to it and generates a compile-time error-
class Data{ static final int num; Data(int i){ num = i; } } public class ConstantsExample { public static void main(String args[]) { System.out.println("value of the constant: ");+Data.num); } }
ConstantsExample.java:4: error: cannot assign a value to final variable num num = i; ^ 1 error
Therefore, it is necessary to initialize the static final variable in the static block.
To make the above program work properly, you need to initialize the final static variable in the static block to-
class Data{ static final int num; static{ num = 1000; } } public class ConstantsExample { public static void main(String args[]) { System.out.println("value of the constant: ");+Data.num); } }
Output Result
value of the constant: 1000