I've been trying to understand the difference between nested, local, anonymous classes and I'm confused about a few things, but mostly why you cannot do this:
class OuterClass {
InnerClass innerClass = new InnerClass() {
//DO SOMETHING
};
}
w/o doing this...
class OuterClass {
class InnerClass {
}
InnerClass innerClass = new InnerClass() {
//DO SOMETHING
};
}
or this...
class OuterClass {
InnerClass innerClass = new InnerClass() {
//DO SOMETHING
};
}
class InnerClass {
}
How is this at all different from this really?
class OuterClass {
InnerClass innerClass = new InnerClass();
innerClass.innerClassMethod();
}
class InnerClass {
public void innerClassMethod() {
}
}
And why is it called anonymous? I dont understand that. Reading this link here I understand the logistical differences (i.e. no name, only 1 instantiation, only accessible where defined, etc. But it's really not a class so why is it called a class? It's actually, from what I can see an instantiation of a class. I see you can have other methods as well from the class it is derived from, but how are you allowed to have completely different methods than the base class w/o having to use @Override?