0

Hi I have a class SomeClass and a private map that I want to have in values list of objects A and B where B extends A.

The below code doesnt compile.. I cant change anything in class name, it must be left like:

public class SomeClass {
<T extends ParentObject> Map<SignedOperationNameEnum, List<T>> operationToOrderStateSkipSignatureSetMap = new HashMap<>();
}
3
  • 1
    Your generic parameter should be on the class: SomeClass<T extends ParentObject> Commented Nov 16, 2015 at 9:52
  • You cannot have generic fields. The type declaration needs to be on either the class or a method. Commented Nov 16, 2015 at 9:55
  • I use java 1.7 and I cand change SomeClass as I said to SomeClass<T extends ParentObject> I can only change method i wrote. Commented Nov 16, 2015 at 11:18

4 Answers 4

1

After class's name definiton

public class SomeClass<T extends ParentObject>  {
Map<SignedOperationNameEnum, List<T>> operationToOrderStateSkipSignatureSetMap = new HashMap<>();
}
Sign up to request clarification or add additional context in comments.

1 Comment

I use java 1.7 and I cand change SomeClass as I said to SomeClass<T extends ParentObject> I can only change method i wrote
0

You should change your code to:

public class SomeClass<T extends ParentObject> {
    Map<SignedOperationNameEnum, List<T>> operationToOrderStateSkipSignatureSetMap = new HashMap<>();
}

2 Comments

I use java 1.7 and I cand change SomeClass as I said to SomeClass<T extends ParentObject> I can only change method i wrote
So you should move operationToOrderStateSkipSignatureSetMap to a function and return it
0

I believe the problem is in the types you are using for instance this code which is very similar to yours compiles and runs perfectly on 1.7.

public class SomeClass {

    static <T extends Number> Map<Integer, List<T>> operationToOrderStateSkipSignatureSetMap() {
        return null;

    }

    public static void main(String[] args) throws IOException {
        System.out.println(operationToOrderStateSkipSignatureSetMap());
    }
}

Comments

0

It depends on java version which u are using. If u are using java 1.7 or higher, then u can use:

 public class SomeClass<T extends ParentObject>  {
        Map<SignedOperationNameEnum, List<T>> operationToOrderStateSkipSignatureSetMap = 
                               new HashMap<>();
    }

If u have 1.6 or earlier, then:

   public class SomeClass<T extends ParentObject>  {
        Map<SignedOperationNameEnum, List<T>> operationToOrderStateSkipSignatureSetMap = 
                               new HashMap<SignedOperationNameEnum, List<T>>();
    }

To introduce method with generics just write:

private <T extends ParentObject> Map<SignedOperationNameEnum, List<T>> operationToOrderStateSkipSignatureSetMap() {
    return new HashMap<>();
}

3 Comments

I use java 1.7 and I cand change SomeClass as I said to SomeClass<T extends ParentObject> I can only change method i wrote
You wrote not a method, but a field, and you cant set generics to your field like u show in your question. To introduce generics to method write: private <T extends ParentObject> Map<SignedOperationNameEnum, List<T>> operationToOrderStateSkipSignatureSetMap() {return new HashMap<>();}
THANKS.. that what I needed

Your Answer

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