2

Like other variables, i want to assign final field type variable to blank but initialization is blocking by Java. What is the its logic? Maybe i want to use default values of my variables? for int = 0, string = null boolean = false etc...

public class Hello {

static final int myNumber; /* it is giving "The blank final field myNumber 
                                may not have been initialized" error in Eclipse */
}
hakki
  • 6,181
  • 6
  • 62
  • 106
  • Because it is `final`. Use final for only those fields that you want to initialized once and thus have a constant value throughout. :) – Lokesh Mehra Nov 24 '12 at 05:53

5 Answers5

4

In Java, after you use the final keyword with a static variable, you must define the value at the point of declaration. You must give it some value and stick with it.

parker.sikand
  • 1,371
  • 2
  • 15
  • 32
3

A final variable can only be initialized once, either via an initializer or an assignment statement. It does not need to be initialized at the point of declaration: this is called a "blank final" variable.

change your code

public class Hello {

final int myNumber; 
 public Hello(int num){
 this.myNumber = num;
}

}

for static final variable use static block for initialization

static{
        myNumber = 0;
    }
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37
  • but if leave blank the value of variable while declaration it is giving error, i can not declare it later. Maybe my Eclipse confused :) – hakki Nov 24 '12 at 05:52
  • you can initialize in later with constructor; – Mohammod Hossain Nov 24 '12 at 05:55
  • one more question @Mohammod Hossain. If constructor can change the value of final with every object call or creation, final (constant) variable can change. Is it very dangerous ? – hakki Nov 24 '12 at 05:58
  • How are you changing a static final in a constructor? You'd have to use a [static initializer](http://stackoverflow.com/questions/335311/static-initializer-in-java). This would work if myNumber weren't static. – beatgammit Nov 24 '12 at 06:12
  • @Perception . which compile type error are you getting in following code? – Mohammod Hossain Nov 24 '12 at 06:36
1

From the Java specs : "It is a compile-time error if a blank final (§4.12.4) class variable is not definitely assigned (§16.8) by a static initializer (§8.7) of the class in which it is declared."

Josep
  • 12,926
  • 2
  • 42
  • 45
0

When you use the "final" modifier with a variable, you must initialize it then as it is the only time you're allowed to use the assignment operator(you can initialize once if you left it blank) on it ("="). Like parker.sikand said, you will get an error if you try to assign a value to it afterwards.

Also, an important note that final STRICTLY means that you cannot use the assignment operator. If the object that is "final" is a mutable object then of course the contents of that object can change without ever using the assignment operator.

EDIT: I believe a final variable has to be guaranteed to be initialized at SOME point in the program which is why you generally initialize it on declaration

Cruncher
  • 7,641
  • 1
  • 31
  • 65
0

Eclipse 2019-09 now proposes a quickfix for that:

Initialize 'final' fields

A Java quickfix is now offered to initialize an uninitialized final field in the class constructor.

https://www.eclipse.org/eclipse/news/4.13/images/finalquickfix1.png

The fix will initialize a String to the empty string, a numeric base type to 0, and for class fields it initializes them using their default constructor if available or null if no default constructor exists.

https://www.eclipse.org/eclipse/news/4.13/images/finalquickfix2.png

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250