1

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.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Steven Roose
  • 2,731
  • 4
  • 29
  • 46
  • What is it that you are actually trying to accomplish here? Meaning what is the actual goal beyond having nested classes, interfaces etc? Seems like you might be overthinking the solution to whatever the real problem is. Interfaces for static methods make no sense as a static method is only declared/exists *once*. – matt b May 14 '12 at 20:48
  • I did it with an Enumeration. Which works fine :) – Steven Roose May 14 '12 at 21:08
  • "But I think just every method in a static class is static," - no this is not true. I think you are confused on the difference between nested classes and inner classes. Read [this](http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html) and [this](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class) – matt b May 14 '12 at 21:08

4 Answers4

6

Interfaces and statics don't go together. At all. There is no Java support for creating / imposing patterns on static methods.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
2

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 NestedClass an interface NestedInterface and store your different implementations as anonymous classes implementing this interface:

    public static final NestedInterface firstNested = new NestedInterface() {
        @Override
        public void method() {
            // ...
        }
    };
    
  • Make NestedClass an enumeration NestedEnum and 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 accept NestedClass implementations from outside sources.

    public enum NestedEnum  {
    
        FIRST {
            @Override
            public void method() {
                // ...
            }
        };
    
        public abstract void method();
    }
    

EDIT: In reply to your comment:

The classes itself are static as well..

static in 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 write new NestedClass1() from within the constructor or an instance method of TopClass, but in this verbose form you can clearly see the dependence on TopClass.this. This can also be seen from any method of NestedClass1, as you have access to the containing class with TopClass.this.

A static nested class such as in your second example can be instantiated through new TopClass.NestedClass1(). Once again, you could just write new NestedClass1() but the verbose form clearly shows that the construction only depends on TopClass and is not associated with an instance of TopClass. You could even create an instance from an outside class using the same snippet new TopClass.NestedClass1() without ever creating a TopClass instance.

I suggest you take a look at this question on inner classes and static nested classes.

Community
  • 1
  • 1
Mattias Buelens
  • 19,609
  • 4
  • 45
  • 51
1

The fact the your interface/abstract class is nested is irrelevant to the problem.
You just can't. There is no way in Java to enforce some class to implement static methods. Just cry and surrender and use instance methods.

Pablo Grisafi
  • 5,039
  • 1
  • 19
  • 29
1

static abstract is a contradiction. Static methods are not like other languages' class methods. When you make a static method it goes on a single class, it doesn't get inherited by or have its implementation deferred to subclasses.

You don't explain why you want these methods to be static. If you want these methods to be defined by subclasses then they shouldn't be.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • Static means that a method's execution is independent of what instance of the class you have. And since the classes just represent methods, this is the case. The classes itself are static as well.. – Steven Roose May 14 '12 at 20:59