Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variable Declaration problem

 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
final reference variables must be initialized before the constructor completes. What does it mean ?
 
John Stark
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's from the chapter I 'Two-minute drill'. I have absolutely no idea what is meant
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It basically means this:
A final variable can be initialised, but can't be changed once initialised. For a final member variable, that initialisation can take place when it's declared, in an initialisation block, or in the contstructor.
 
John Stark
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes it clear.

John
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:It basically means this:
A final variable can be initialised, but can't be changed once initialised. For a final member variable, that initialisation can take place when it's declared, in an initialisation block, or in the contstructor.


thank you matthew for detailed explanation .
 
Rumesh Eranga
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:It basically means this:
A final variable can be initialised, but can't be changed once initialised. For a final member variable, that initialisation can take place when it's declared, in an initialisation block, or in the contstructor.


This is a very good explanation. Thank you Matthew Brown
 
John Schubert
Ranch Hand
Posts: 39
Android Java
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:It basically means this:
A final variable can be initialised, but can't be changed once initialised. For a final member variable, that initialisation can take place when it's declared, in an initialisation block, or in the contstructor.


Well I don't know if it is that ok to assign a String to an int.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:It basically means this:
A final variable can be initialised, but can't be changed once initialised. For a final member variable, that initialisation can take place when it's declared, in an initialisation block, or in the contstructor.


Did you mean,compiler will look all methods after constructor and initialization block?
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, if I change Mattew's code into this:


Here are the steps when new FinalTest() is invoked:
1. FinalTest() constructor is called.
2. Implicilty the super() contructor, which is the Object's constructor is called.
3. When Object's constructor is completed, FinalTest constructor is completed.
4. value is assigned to 10.

Are these the correct steps?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic