Access modifiers ignored !?!
843798Jun 24 2001 — edited Jun 25 2001Hi
please, have a look at the code below and let me know your opinions
// ------------- source file 1 ------------------------
package pckg1;
public class Dog {
final public static void main (String[] args) {
pckg2.Puppy puppy = new pckg2.Puppy("Spot");
System.out.println(puppy.getName());
}
}
// ------------- source file 2 --------------------
package pckg2;
class Puppy {
String name;
public Puppy(String name) {
this.name = name;
}
String getName() {return name;}
}
Each class belongs to a different package. Obviously if you compile Puppy first and attempt compiling Dog the compiler will complain about several things (class Puppy not being public, method getName() not being public, etc...).
Now,
if you change the Puppy class so that the necessary bits are public, re-compile Puppy and then Dog it will work.
When you subsequently go back to Puppy and remove the added public access modifiers and re-compile JUST the Puppy class and try running Dog IT WILL STILL WORK AS IF EVERYTHING IN THE PUPPY CLASS WERE PUBLIC.
I don't thing this is right. If the access modifiers cannot be relied on then the whole system is flawed.
PS: Running on W2K, the behaviour observed on JRE 1.2.2_007, 1.3.0_02 as well as on 1.4.0-beta-b65.
Cheers
Ales Krestan