aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final variable" Watch "final variable" New topic
Author

final variable

Bhavana Gupta
Greenhorn

Joined: Dec 14, 2000
Posts: 6
What will be the output when you compile and execute the following program.
////////////////////////////////////////////
01: class Base
02:{
03: static final int MAX_SIZE;
04: Base(){
05: MAX_SIZE = 10;
06: }
07:
08: void test() {
09: MAX_SIZE++;
10: System.out.println("Base.test()");
11: }
12:
13:}
Select all valid answers.
a) Compilation Error at Line 03: Blank final variable 'MAX_SIZE' may not have been
initialized. It must be assigned a value in an initializer, or in every constructor.
b) Compilation Error at Line 05: Can't assign a second value to a blank final variable:
c) Compilation Error at Line 09: Can't assign a second value to a blank final variable:
d) No errors
The ans they give is A B C
I wrote was C as i think when a var is final then it can be assigned a val in constructors if not given any value while declaring.
------------------
Find A Purpose In Life So Big That It Will Challenge Every Capacity To Be At Your Best.


Find A Purpose In Life So Big That It Will Challenge Every Capacity To Be At Your Best.
Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
Since static members are initialized during class loading time they must be assigned a value (if they are final too) during the same period of time..
so they should be assigned either when they are declared or in static free floation blocks.
[This message has been edited by Nasir Khan (edited December 15, 2000).]
 
 
subject: final variable