1

I have problem with sets. Required java.lang.String found String... What can i do there?

public interface Node {
        public <V> V get();
        public <V> void sets(V value);
    }

public enum MIBNodes implements Node {

    TEST {
         private String e;
        @Override
        public String get() {
            return "aa";
        }

        @Override
        public <String> void sets(String value) {
           e=value;
        }




    };


};

UPDATE
Each enum instance like TEST , TEST1 ... may have different type.. String, Integer or anyother... So public enum MIBNodes implements Node { cant become public enum MIBNodes implements Node<String> {

1 Answer 1

4

This is the Problem:

@Override
public <String> void sets(String value) {
        ^^^^^^
    e=value;
}

Here, String is a type variable (a re-definition of V), not a java.lang.String. And I don't really think you can fix that without changing your design:

public interface Node<V> {
    public V get();
    public void sets(V value);
}

And in case you want your enum to be generic : that's impossible. Different enum items can't implement the same interface with different generic parameters.

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

3 Comments

So any alternative solution to this?
@Parhs One enum per Generic Type, I'm afraid
Of course it works for get: you can always return a more specific type (and document that in the method signature). But you can't take a more specific parameter than the super class or interface

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.