saravanan
saravanan

Reputation: 5407

Why must a final variable be initialized before constructor completes?

Why must a final variable be initialized before constructor completes?

public class Ex
{
  final int q;
}

When I compile this code I get error like this

err:variable q might not have been initialized

Upvotes: 47

Views: 71933

Answers (10)

Emmanuel
Emmanuel

Reputation: 344

The moment the constructor completes execution, the object is 'open for business' - it can be used, its variables can be accessed.

The final keyword on a variable guarantees that its value will never change. This means, if the value is ever read, you can be sure that the variable will always have that value.

Since the variable can be accessed (read) at anytime after the execution of the constructor, it means it must never change after the constructor has executed - just it case it has been read.

Thus, it must, by then, have been set.

Upvotes: 0

UVM
UVM

Reputation: 9914

Final modifier does not allow to change your variable value. So you need to assign a value to it at some place and constructor is the place you have to do this in this case.

Upvotes: 0

Dikshita Goyal
Dikshita Goyal

Reputation: 1

If an instance variable is declared with final keyword, it means it can not be modified later, which makes it a constant. That is why we have to initialize the variable with final keyword.Initialization must be done explicitly because JVM doesnt provide default value to final instance variable.Final instance variable must be initialized either at the time of declaration like:

class Test{
   final int num = 10;
   }

or it must be declared inside an instance block like:

class Test{
 final int x;
  {
   x=10;
  }
} 

or it must be declared BEFORE constructor COMPLETION like:

class Test{
 final int x;
  Test(){ 
   x=10;
  }
}

Keep in mind that we can initialize it inside a consructor block because initialization must be done before constructor completion.

Upvotes: 0

assylias
assylias

Reputation: 328598

The official reason is that it is defined by the Java Language Specification 8.3.1.2:

A blank final instance variable must be definitely assigned at the end of every constructor of the class in which it is declared; otherwise a compile-time error occurs.

A blank final is a final variable whose declaration lacks an initializer (i.e. what you describe).

Upvotes: 38

Kendall Frey
Kendall Frey

Reputation: 44326

A final variable must be initialized at the declaration or in a constructor.

If it has not been initialized when the constructor returns, it may never be initialized, and may remain an uninitialized variable. The compiler cannot prove it will be initialized, and thus throws an error.

This Wikipedia excerpt explains it well:

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. A blank final instance variable of a class must be definitely assigned at the end of every constructor of the class in which it is declared; similarly, a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared: otherwise, a compile-time error occurs in both cases. (Note: If the variable is a reference, this means that the variable cannot be re-bound to reference another object. But the object that it references is still mutable, if it was originally mutable.)

Upvotes: 11

jopasserat
jopasserat

Reputation: 5920

Because final prevents you from modifying variables, but it has to be initialized at some point, and the constructors is the right place to do so.

In your case, it would be called a blank final because it is not initialized when declared.

Upvotes: 15

Miquel
Miquel

Reputation: 15675

The final keyword applied to a field has one of two effects:

  • on a primitive, it prevents the value of the primitive from being changed (an int can't change value)
  • on an object, it prevents the "value of the variable", that is, the reference to the object, from being changed. That is to say that, if you have a final HashMap<String,String> a, you will only be able to set it once, and you won't be able to do this.a=new HashMap<String,String>(); again, but nothing keeps you from doing this.a.put("a","b"),s since that doesn't modify the reference, only the content of the object.

Upvotes: 6

David Webb
David Webb

Reputation: 193696

The value of a final variable can only be set once. The constructor is the only place in the code for a class that you can guarantee this will hold true; the constructor is only ever called once for an object but other methods can be called any number of times.

Upvotes: 15

Michał Kosmulski
Michał Kosmulski

Reputation: 10020

The language specification contains specific guarantees about the properties of final variables and fields, and one of them is that a properly constructed object (i.e. one whose constructor finished successfully) must have all its final instance fields initialized and visible to all threads. Thus, the compiler analyzes code paths and requires you to initialize those fields.

Upvotes: 0

Adam Monos
Adam Monos

Reputation: 4297

The final modifier prevents your from changeing the variables value, hence you have to initialize it where you declare it.

Upvotes: 2

Related Questions