Just wondering if this is the right way to do it. I want to construct instances of my parametrized class where one of the instance variables is a generic type. The code below works but I get a lot of warnings in the main method "SomeObject is a raw type. References to generic type SomeObject should be parameterized".
public class SomeObject<T> {
private String description;
private T value;
public SomeObject(String description, T value) {
this.description = description;
this.value = value;
}
public static void main(String args[]){
List <SomeObject> objectList = new ArrayList<SomeObject>();
objectList.add(new SomeObject("Object 1: ", true));
objectList.add(new SomeObject("Object 2: ", 888.00));
objectList.add(new SomeObject("Object 3: ", "another object"));
objectList.add(new SomeObject("Object 4: ", '4'));
for (SomeObject object : objectList){
System.out.println(object.getDescription() + object.getValue());
}
}
}
The code below works but I get a lot of warnings in the main method "Object is a raw type. References to generic type Object should be parameterized".
the warning is because you haven't specified the type arguments when constructing the SomeObject
. ie.
it should be:
List<SomeObject<?>> objectList = new ArrayList<>();
objectList.add(new SomeObject<Boolean>("Object 1: ", true));
objectList.add(new SomeObject<Double>("Object 2: ", 888.00));
objectList.add(new SomeObject<String>("Object 3: ", "another object"));
objectList.add(new SomeObject<Character>("Object 4: ", '4'));