Static and non static blank final variables in Java
A variable provides us with named storage that our programs can manipulate. There are two types of data variables in a class:
Instance variables : Instance variables are declared in a class, but outside a method, constructor or any block. When a space is allocated for an object in the heap, a slot for each instance variable value is created. Instance variables are created when an object is created with the use of the keyword ‘new’ and destroyed when the object is destroyed. They are property of an object so they can be accessed using object only.
Static variables : Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block. There would only be one copy of each class variable per class, regardless of how many objects are created from it. They are property of a class not of an object so they can be used directly using class name as well as using object.
Name: Shubham Company: GeeksForGeeks Salary: 100000 Name: Chirag Company: GeeksForGeeks Salary: 200000 After change Name: Shubham Company: Google Salary: 200000 Name: Chirag Company: Google Salary: 200000
In the above example, by changing the company name it is reflected in all other objects as it is a static variable. But changing the salary of g doesn’t change the salary of s because salary is an instance variable.
Blank final variable : A final variable declared but not assigned is known as a blank final variable. It can be initialized within a constructor only. It raises compilation error if it is not initialized because it should be given a value somewhere in the program and that too from a constructor only.
Static blank final variable : It is a blank final variable declared as static. That is, a final static variable declared but not given a value or not initialized is known as static blank final variable.It can be initialized through a static block only.
Here is an example illustrating initialization of blank final variables-
1 10 10
In the above example, b is initialized using constructor while a using static block.
Predict the output of the following program :
prog.java:8: error: variable USER_ID might already have been assigned USER_ID = GUEST_ID; ^ 1 error
The USER_ID field is a static blank final. It is clear that the exception can be thrown in the try block only if the assignment to USER_ID fails, so it is perfectly safe to assign to USER_ID in the catch block. Any execution of the static initializer block will cause exactly one assignment to USER_ID, which is just what is required for blank finals. But this program fails because, A blank final field can be assigned only at points in the program where it is definitely unassigned.
Here, compiler is not sure whether its been assigned in try block or not, so the program doesn’t compile. We can solve this by removing the static block and initializing the USER_ID at the time of declaration.
Logging in as guest User ID: -1
This article is contributed by Shubham Juneja. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.