Friday, October 16, 2009

SCJP exercise : private, protected and public

Exercise :

public class Test {

public int myTest = 2;
private String str;
public static void main(String [] args) {
System.out.println("myTest=" + myTest);



What is the result ? (choose Two)

A myTest=2
B myTest=0
C Compilation fails
D An exception is thrown at runtime


C . The compilation will fail because we are in the main() method and we haven't instantiate Test.To make it working on solution should be :
Test monTest = new Test();
System.out.println("monTest=" + monTest.myTest);

No comments:

Post a Comment