No James...they are not useless,
here is what I've found !!
consider this code :
// File: abc/Q.java
package abc;
public class Q {
protected int protectedVar;
}
and this code is in the default package:
// File: Tester.java
import abc.Q;
public class Tester {
private Q q = new Q() {
void someMethod() {
protectedVar = 1; // will this compile?
}
};
}
see..., class Tester does not extends Q, and both class are in the different package...!
Both classes compile cleanly. You may wonder why the assignment statement seems to compile cleanly since it appears that a protected member is being accessed from within a class (Tester) that does not extend the class that declared the protected member (class Q). Specifically, it may seem strange that class Tester has access to the protected member, protectedVar, of class Q even though Tester is not a subclass of Q and the two classes are in different packages.
In actuality, the reference to protectedVar is not taking place within Tester at all! Note the declaration of the variable q within Tester. q is an instance of a subclass of Q. What's the name of this subclass? Well, it doesn't have a name -- it's an anonymous inner class. The important thing to realize is that since q is an instance of a subclass of Q, it has access to the protected members in Q.
The result of compiling the above code is three separate class files: Q.class, Tester.class, and Tester$1.class. Tester$1 is the name the
Java compiler assigned to the anonymous inner class of Q.
hope that helps
stevie