• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static....

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test
{
final int y; //1

static { y = 10; } //2

{ y = 20; } //3

public Test()
{
y = 0; //4
}

public Test(int i)
{
y = i; //5
}
}
A : Comment out line //2
B : Comment out line //2, //3
C : Comment out line //2, //3 and //4
D : Comment out line //2, //4 and //5
E : Comment out line //3, //4 and //5
F : No need, the code compiles properly


Explain......Plz....
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is B (and D)

2: because y is an instance variable and is not accessible from a static context.
3: while not "wrong" it will cause an error in both constructors because it will assign a value to y, which is final, and then the constructors will try to assign another value (think of what "final" means). The compiler catches this (twice: at 4 and 5) and tells you that the variable has already been asigned (at 3).

D is also correct.

2: see above
4/5: As an alternative to commenting out 3 for the same reasons given above.
[ June 26, 2006: Message edited by: Peter MacMillan ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic