Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final variable

 
Bhavana Gupta
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nasir Khan
Ranch Hand
Posts: 135
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic