GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Variable Declaration problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Variable Declaration problem" Watch "Variable Declaration problem" New topic
Author

Variable Declaration problem

Nitesh Nandwana
Ranch Hand

Joined: Jun 07, 2011
Posts: 115

final reference variables must be initialized before the constructor completes. What does it mean ?
John Stark
Ranch Hand

Joined: Jul 19, 2011
Posts: 185
That's from the chapter I 'Two-minute drill'. I have absolutely no idea what is meant
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

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

Joined: Jul 19, 2011
Posts: 185
That makes it clear.

John
Nitesh Nandwana
Ranch Hand

Joined: Jun 07, 2011
Posts: 115

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

Joined: Jan 26, 2011
Posts: 34
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

Joined: Sep 21, 2011
Posts: 39

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

Joined: Feb 09, 2012
Posts: 583

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

Joined: Nov 01, 2011
Posts: 451
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?
 
GeeCON Prague 2014
 
subject: Variable Declaration problem