File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final reference variables

 
venkatesh badrinathan
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"final reference variables must be initialized before the constructor completes".
Can anyone give the reason for this.. please
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks mark...
 
Wout Er
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9486
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic