I was successfully able to excecute c05:LibTest.java (the java program in chapter 5) given below.
//: c05:LibTest.java
// Uses the library.
import com.bruceeckel.simpletest.*;
import com.bruceeckel.simple.*;
publicclass LibTest {
static Test monitor = new Test();
publicstaticvoid main(String[] args) {
Vector v = new Vector();
List l = new List();
monitor.expect(new String[] {
"com.bruceeckel.simple.Vector",
"com.bruceeckel.simple.List"
});
}
} ///:~
The Vector.java and List.java are present in the com/bruceeckel/simple directory and are given below
//: com:bruceeckel:simple:Vector.java
// Creating a package.
package com.bruceeckel.simple;
publicclass Vector {
public Vector() {
System.out.println("com.bruceeckel.simple.Vector");
}
} ///:~
//: com:bruceeckel:simple:List.java
// Creating a package.
package com.bruceeckel.simple;
publicclass List {
public List() {
System.out.println("com.bruceeckel.simple.List");
}
} ///:~
Now in Vector.java I have removed the public access specifier for the Vector class and have made the constructor private as below.
//: com:bruceeckel:simple:Vector.java
// Creating a package.
package com.bruceeckel.simple;
class Vector {
private Vector() {
System.out.println(
"com.bruceeckel.simple.Vector");
}
} ///:~
I have compiled the LibTest.java and thought it shouldnt work because the constructor was declared private but it compiled and executed safely. Why is it so? If that is the case then even this program given below shouldnt complain
//: c05:IceCream.java
// Demonstrates "private" keyword.
class Sundae {
private Sundae() {}
static Sundae makeASundae() {
returnnew Sundae();
}
}
publicclass IceCream {
publicstaticvoid main(String[] args) {
Sundae x = new Sundae();
//Sundae x = Sundae.makeASundae();
}
} ///:~
Can anyone explain me the difference in behavior between the LibTest.java and and the Icecream.java programs?