aspose file tools*
The moose likes Beginning Java and the fly likes final reference variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "final reference variables" Watch "final reference variables" New topic
Author

final reference variables

venkatesh badrinathan
Ranch Hand

Joined: Aug 03, 2008
Posts: 77
"final reference variables must be initialized before the constructor completes".
Can anyone give the reason for this.. please


SCJP1.5
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Think about it logically... a final means that once the variable is set, it cannot change. It's constant for the life of the object. So what would happen in the following code if we waited to initialize the value of a final variable?



Trace through the code. What would happen to "SOME_CONSTANT" if this were legal? Would its value be final?

Now compare that to this:



Trace through the code. Now the value of the final is truly final. Why?

Remember, for specific instance of an object, the constructor can only be called once.

Make sense?
[ August 10, 2008: Message edited by: Mark Vedder ]
venkatesh badrinathan
Ranch Hand

Joined: Aug 03, 2008
Posts: 77
Thanks mark...
Wout Er
Greenhorn

Joined: Nov 26, 2012
Posts: 15
I also saw this in the SCJP6 Study Guide. Any idea why it doesn't say "final reference variables must be initialized before the constructor completes"? (in the context of instance variables)
Afaik, final primitive instance variables also need to be initialized before the constructor completes...
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7053
    
  16

Wout Er wrote:Afaik, final primitive instance variables also need to be initialized before the constructor completes...

Actually, if you don't initialize them, I think they will be given a default value (eg, 0) - but I wouldn't stake my life on it. What you can't do is set them anywhere outside the constructor or in place.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Winston Gutkowski wrote:Actually, if you don't initialize them, I think they will be given a default value (eg, 0) - but I wouldn't stake my life on it.

Only non-final member variables are initialized to default values when you don't initialize them explicitly. Final member variables must be explicitly initialized - if you don't, the compiler will complain.

As far as I know there is no difference between variables of primitive and non-primitive types with regard to this.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: final reference variables
 
Similar Threads
final primitive variable initialization
Final reference variables Initialization
Why Java ?
Final variable
Variable Declaration problem