File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Static....