This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static...." Watch "Static...." New topic
Author

Static....

Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
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....


$Hi/\bEr
Peter MacMillan
Ranch Hand

Joined: Jun 23, 2006
Posts: 34
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static....
 
Similar Threads
Initialization of Interfaces
Can some explain it to me please?
Local variables
mock exam's doudt
A Static Question which is worth to know