I have a Java problem with nested classes.
My first class structure looked like this:
public class TopClass {
public void mainMethod() {
// uses the different "method" methods from
// NestedClass-implementing nested classes
}
private interface NestedClass {
public void method();
}
private class NestedClass1 {
public void method() {
}
}
private class NestedClass2 {
public void method(){
}
}
}
But now I want these method() methods to be static because they should be principally.
I cannot make them static without having them in a static class, but that’s no problem, I made the classes static, they should be anyway.
It looks like this right now:
public class TopClass {
public void mainMethod() {
// uses the different "method" methods from
// NestedClass-implementing nested classes
}
private static interface NestedClass {
public void method();
}
private static class NestedClass1 {
public static void method() {
}
}
private static class NestedClass2 {
public static void method(){
}
}
}
But then the trouble begins. A static method does not inherit correctly from a non-static interface method, as I get this message This static method cannot hide the instance method from TopClass.NestedClass in Eclipse.
When I make the interface method static, it gives me this error: Illegal modifier for the interface method method; only public & abstract are permitted
So I thought of an abstract class, and tried this:
public class TopClass {
public void mainMethod() {
// uses the different "method" methods from
// NestedClass-implementing nested classes
}
private static abstract class NestedClass {
public static abstract void method();
}
private static class NestedClass1 {
public static void method() {
}
}
private static class NestedClass2 {
public static void method(){
}
}
}
But again, seemingly abstract methods cannot be declared static: The abstract method method in type NestedClass can only set a visibility modifier, one of public or protected.
Leaving the static away (in the abstract class method), errors this on the method methods in the NestedClass1 & 2: This static method cannot hide the instance method from TopClass.NestedClass.
Isn’t there any way to declare some kind of superstructure for covering static methods?
EDIT:
The problem I actually try to solve it the lack of possibility of Java for storing references to methods. So instead I have those classes everyone with just one method, but to store them in a List f.e. they must be able to be “caught” by a superstructure.
I got the hint to try anonymous classes or enums, gonna try that now.
A static method declaration must always be followed by a definition. It cannot be implemented by subclasses.
I think you’re just not approaching your problem right. Try a different approach!
Make
NestedClassan interfaceNestedInterfaceand store your different implementations as anonymous classes implementing this interface:Make
NestedClassan enumerationNestedEnumand store your different implementations as enumeration values implementing an abstract method from the enumeration. This only works if you have a fixed number of implementations you which to choose from and you do not want to acceptNestedClassimplementations from outside sources.EDIT: In reply to your comment:
staticin the context of a nested class means that this class can be instantiated without an instance of the containing class.A regular nested class such as in your first example can be instantiated through
TopClass.this.new NestedClass1(). Normally you’d simply writenew NestedClass1()from within the constructor or an instance method ofTopClass, but in this verbose form you can clearly see the dependence onTopClass.this. This can also be seen from any method ofNestedClass1, as you have access to the containing class withTopClass.this.A static nested class such as in your second example can be instantiated through
new TopClass.NestedClass1(). Once again, you could just writenew NestedClass1()but the verbose form clearly shows that the construction only depends onTopClassand is not associated with an instance ofTopClass. You could even create an instance from an outside class using the same snippetnew TopClass.NestedClass1()without ever creating aTopClassinstance.I suggest you take a look at this question on inner classes and static nested classes.