my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes assignment of final member 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 "assignment of final member variables" Watch "assignment of final member variables" New topic
Author

assignment of final member variables

Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
I'm through reading the "Complete Java 2 Certification Study Guide 2nd Ed" (S.Roberts et al) and I've begun with the supplied Bonus mock exams. In one of the mock exam, the follwoign question comes up:
What happens when you attempt to compile and execute the following application ?
1. class A {
2. final int y;
3. public static void main(String args[]) {new A();}
4. A() { y= 5;}
5. }
A) Compiler error at line 2
B) Compiler error at line 3
C) Compiler error at line 4
D) Exception thrown at line 4
E) The code compiles with no errors and runs without throwing any exceptions
My guess was C, because y, although being final, is a member variable which
gets initialized to 0 BEFORE an instance of A is created. Then, I thought
that assigning 5 to y in the constructor was illegal since 0 had already
been assigned to y.
The author's comment: Correct selection is E.
A final variable does not need to have its value assigned on the same line
where it is declared.
I'm perfectly ok with the comment, but the fact is that y is a member variable
and should already be initialized to 0 BEFORE the constructor of the class A
gets called and thus the assignement in the constructor is illegal. I think this
would be ok if y was an automatic variable since those are not automatically
initialized.
Does this all makes sense ??
Thank you for your answers,
Val


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Christy Smith
Greenhorn

Joined: Aug 15, 2001
Posts: 26
Java allows final instance variable to be initialized in the constructor before it is used. In addition, final instance variable needs to be initialized explicitly. If you change the code to the following:
1. class A {
2. final int y;
3. public static void main(String args[]) {new A();}
4. A() { //y= 5;}
5. }
The following compilation error will show up:
variable y might not have been initialized
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Just because y's default value is 0 does not mean y was initialized to 0. Java gave y a default value of 0, which can be initialized only once to whatever value an int can take.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Valentin,
Finals are a special case; they are never set to their default values. Which is why the must be explicitly initialized, either when the declared, in an initializer block or, for instance members, in every constructor.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Karma Police
Greenhorn

Joined: Aug 23, 2001
Posts: 11
Try to compile all the codes I list below then you will find the use of the key word "final".

Code A It will compile and run successfully)



Code B Thye compiler will complain "Test5.java:2: Blank final variable 'y' may not have been initialized. It must be assigned
a value in an initializer, or in every constructor.
static final int y;
^
Test5.java:8: Can't assign a second value to a blank final variable: y
y = 5;
^
2 errors")



Code C The compiler will complain "Test5.java:10: Can't assign a second value to a blank final variable: y
y = 10;
^
1 error")



Code D The compiler will complain "Test5.java:2: Blank final variable 'y' may not have been initialized. It must be assigned
a value in an initializer, or in every constructor.
final int y;
^
1 error")


------------------
Java Lover From Hell!


Java Lover From Hell!
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi all,
You have to assign a value for static final variables
when you declare them or
in the static initializer of the class.
Quote from JLS 8.3.1.2

8.3.1.2 final Fields
A field can be declared final (�4.5.4). Both class and instance variables (static and non-static fields) may be declared final.
It is a compile-time error if a blank final (�4.5.4) class variable is not definitely assigned (�16.7) by a static initializer (�8.7) of the class in which it is declared.
A blank final instance variable must be definitely assigned (�16.8) at the end of every constructor (�8.8) of the class in which it is declared; otherwise a compile-time error occurs.


Hope this helps,
Vanitha.
[This message has been edited by Vanitha Sugumaran (edited August 27, 2001).]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Thank you all for your quick answers. They helped me a lot in the understanding of final variables. I could have read the specific part about final variables in the JLS before asking, though ! I'll remember that for next time.
Again, thank you all.
Val
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Karma ...
What are you using to compile your code? The first example compiles without error and prints "y=5".
Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.
Thanks for your cooperation.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: assignment of final member variables
 
Similar Threads
Sun Cirtification
Static....
javaranch's java Rule Round-up
Where to find ans for mock test by Valentinn Cretazz
Mughal Mock Exam Doubt.