aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Blank Final Variables 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 "Blank Final Variables" Watch "Blank Final Variables" New topic
Author

Blank Final Variables

Swati Singhal
Ranch Hand

Joined: Dec 08, 2003
Posts: 31
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
Thomas De Vos
stable boy
Ranch Hand

Joined: Apr 12, 2003
Posts: 425
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 ]

Try your free <a href="http://www.javacertificate.com" target="_blank" rel="nofollow">SCJP 1.4</a> certification centre.<br />Try your free <a href="http://www.j2eecertificate.com" target="_blank" rel="nofollow">SCWCD</a> certification centre.<br />Try your free <a href="http://www.ejbcertificate.com" target="_blank" rel="nofollow">SCBCD</a> certification centre.<br />Try your <a href="http://www.webspherecertificate.com" target="_blank" rel="nofollow">Websphere (Test 285) </a> certification centre.<br />Try your <a href="http://www.j2mecertificate.com" target="_blank" rel="nofollow">SCMAD</a> certification centre. (New)<br /> <br /><a href="http://blogs.javacertificate.com" target="_blank" rel="nofollow">Java/J2EE Certification Blogging</a>
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
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 ]

"Eppur si muove!"
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Swati
Maybe this thread will help you.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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"


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Blank Final Variables