0

I'm trying to create an interface with a bounded type parameter, with implementations supplying static nested classes to implement this parameter, as follows:

public interface InterfaceProblem<T extends IMyParameter>{
    T getParameterInstance();
}

interface IMyParameter {}

class MyClass implements InterfaceProblem<MyParameter> {
    public MyParameter getParameterInstance() {
        return new MyParameter();
    }

    class MyParameter implements IMyParameter{}
}

This gives me a compile error "MyParameter cannot be resolved to a type" on the MyClass declaration and its method. This disappears if I move the static class to its own type:

class MyClass implements InterfaceProblem<MyParameter> {
    public MyParameter getParameterInstance() {
        return new MyParameter();
    }
}

class MyParameter implements IMyParameter{}

However, I'd like to avoid that, since the MyParameter implementation is closely related to the MyClass implementation. Is there a better way I can acheive this? Is this correct compiler behaviour? (I'm using Eclipse Mars and Oracle jdk1.8.0_60)

1
  • 'static inner' is a contradiction in terms. This is a static nested class. Commented Nov 7, 2015 at 9:14

1 Answer 1

1

You're missing an import:

import com.example.MyClass.MyParameter;

While the MyParameter type is on scope for the getParameterInstance() method's return type, it is not for the MyClass's binding of <T>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.