4 ms·
In practice, it's not the same thing. Here's a Java example where I want the type system to enforce that a method in `ClassB` can only be called from `ClassA`.
by greydius 10y ago
In practice, it's not the same thing.
Here's a Java example where I want the type system to enforce that a method in `ClassB` can only be called from `ClassA`. However, the fact that `null` circumvents the type system makes this pattern just wishful thinking.
class ClassA {
private static final Witness witness = new Witness();
final static class Witness {
private Witness() {}
}
void callClassBMethod(ClassB classB) {
classB.onlyClassACanCallThisMethod(witness);
}
}
class ClassB {
void onlyClassACanCallThisMethod(ClassA.Witness witness) {
// ...
}
}