Sunday, 19 July 2009

Java Certification Question 0039

Given the following class definition
public class Droitwich{

class one{
private class two{
public void main(){
System.out.println("two");
}
}
}
}

Which of the following statements are true

1) The code will not compile because the classes are nested to more than one level
2) The code will not compile because class two is marked as private
3) The code will compile and output the string two at runtime
4) The code will compile without error



Answer :
4) The code will compile without error
There are no restrictions on the level of nesting for inner/nested classes. Inner classes may be marked private. The main method is not declared as public static void main, and assuming that the commandline was java Droitwich it would not be invoked anyway.

No comments: