• Post Reply Bookmark Topic Watch Topic
  • New Topic

Final reference variables in Java  RSS feed

 
radha gogia
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am unable to understand the meaning of this sentence "final reference variables must be initialized before the constructor
completes.",What is trying to imply?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would help to have some context, but I'm guessing it means that the following won't compile:
because x is a final instance variable, and it isn't initialised. You can fix that either by initialising it in the constructor, or by initialising it on the same line that it is declared (which is done before the constructor runs). But it has to have a value before the object is fully instantiated.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
radha gogia wrote:"final reference variables must be initialized before the constructor completes."

The answer is given in the above sentence.

As Matthew wrote, the x must be initialised either straight away, or latest within a constructor, because it can be initialised only once.
If you wouldn't initialise latest in a constructor, in theory by default constructor should initialise it to a null (which would be wrong, as you wouldn't be able to change value later on).
Sorry, i'm still sick, can't express my minds properly

 
radha gogia
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir,that is clear to me for instance variables ,but I am talking about reference variables ,if I have a final reference variable like
class abc
{
public static void main(String aa[])
{
final abc obj=new abc(); // obj is a reference variable
}
}

So ,now what is the statement trying to explain ,I am totally confused in this.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are confusing yourself because you are mixing terminology. Do you actually mean member variables instead of "reference" variables?

Reference variables are variables of non-primitive types. Member variables as well as local variables can be either reference variables or primitive type variables.

Final member variables must be initialized before the constructor completes. The type of the variables doesn't matter - it doesn't matter if those variables are of a primitive type or a reference type.
 
radha gogia
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir,I am unable to get the fact that isn't it true that the initialization of all the reference variables is complete after the constructor call completes,but if I have a final reference variable then does it means that the initialization of its instance variables is complete after the constructor call.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the time being forget about the difference between reference types and primitives.

You ought to initialise all instance fields by the time the constructor completes. The compiler can only enforce that for final fields. Final static fields must be initialised on declaration or in a (static) initialiser block.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also bear in my that local variables and instance are two different categories.

obj is a local variable here (local to the method) init'ed within its declaration:


Whereas here x is an instance member.
Being final, it must be init'ed before the constructor returns:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!