• 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

Blank Final Variables

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I read somewhere that a Blank Final Variable should be assigned a value before the end og a constructor.
Can somebody please throw some light on this?
Thanks
 
stable boy
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A "Blank" final variable has to be initialized before it is used, be it in the constructor or somewhere else in the code.
Remember once your final variables have been initialized you cannot re-assign a value to it.
Practical: I recommend initializing any final variable where it is declared, no confusion.
[ March 22, 2004: Message edited by: Thomas De Vos ]
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Originally posted by Thomas De Vos:
A "Blank" final variable has to be initialized before it is used, be it in the constructor or somewhere else in the code.


I think blank final variables have always to be initialized, even if they are not used. They are implicitly initialized but an assignment before the end of the constructor is required (see the code below)


class A {
A(){foo();}
void foo(){}}
public class B extends A {
final int i;
B(){i=10;
}
void foo(){
System.out.println("i: " + i
);
}
public static void main(String [] args) {
B t = new B();
t.foo();
}
}


Greetings,
Gian Franco
[ March 22, 2004: Message edited by: Gian Franco Casula ]
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Swati
Maybe this thread will help you.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For people eager to learn more about final fields, you can check out Chapter 2 - The Final Story of O'Reilly's new book "Hardcore Java"
 
reply
    Bookmark Topic Watch Topic
  • New Topic