• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Final reference variables in Java

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic