barbara
barbara

Reputation: 3201

Java: How to make interfaces types interconnected?

How can I get connected a type I of interface Parser to validate method parameter type?

public interface Parser<R, I> {
    R parse(I... input);

    interface Validator {
        boolean validate(I... content);
    }
}

Right now, I get an error:

`my.pack.Parser this`: cannot be referenced from a static context

UPD

If I add a type <I> to my Validator interface I still don't have types relationship.

Parser<String, String> p = new Parser<String, String>() {
    @Override
    public String parse(String... input) {
        Validator<Integer> v = new Validator<Integer>() {
            @Override
            public boolean validate(Integer... content) {
                return false;
            }
        };
        return null;
    }
};

In my example, the type of Validator should be the same as in Parser - String, but not Integer.

Upvotes: 0

Views: 43

Answers (1)

user4759923
user4759923

Reputation: 531

You have to specify the type I as a generic parameter to the Validator interface because inner interfaces are always static, so the Validator cannot access the I which is not defined for the interface Parser but only when you instantiate it.

Upvotes: 1

Related Questions