The following question is taken from
http://www.jchq.net/mockexams/exam3.htm public class Sandys{
private int court;
public static void main(
String argv[]){
Sandys s = new Sandys(99);
System.out.println(s.court);
}
Sandys(int ballcount){
court=ballcount;
}
}
1) Compile time error, the variable court is defined as private
2) Compile time error, s is not initialized when the System.out method is called
3) Compilation and execution with no output
4) Compilation and run with an output of 99
The correct answer is 4. However, why it is not 1 since the variable is private, we cannot access it using s.court. That's why we have the getters and setters method, am I right?
Andy