Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

final variable

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
A wop bop a lu bop a womp bam boom! Tiny ad:
The trailboss has a kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic