• 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

Variable Declaration problem

 
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
final reference variables must be initialized before the constructor completes. What does it mean ?
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's from the chapter I 'Two-minute drill'. I have absolutely no idea what is meant
 
Bartender
Posts: 4568
9
  • Likes 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That makes it clear.

John
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 50
Android Java
  • Likes 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic