1

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());
    }
}

}

6
  • 1
    What part of the error don't you understand? You should actually use your generics. Commented Apr 25, 2017 at 15:27
  • 1
    should it be new SomeObject() instead of new Object()? Commented Apr 25, 2017 at 15:28
  • 1
    Try parameterizing the object creation like you have done for arraylist Commented Apr 25, 2017 at 15:32
  • "should it be new SomeObject() instead of new Object()? " Yes it should. I edited it. Thanks. Commented Apr 25, 2017 at 15:35
  • Just a side note: in Java 7 and later, you can often omit the type in the second set of angle brackets, and let it be inferred. It is called the diamond operator. Commented Apr 25, 2017 at 15:37

2 Answers 2

4

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'));
Sign up to request clarification or add additional context in comments.

2 Comments

Can you add the declaration of objectList with type to complete your answer?
@RC. sure thing.
2

When you have a SomeObject without a type argument (the part in the square brackets), that is called a raw type, and it's the same as using the erasure of SomeObject. (Basically, the erasure means it's non-generic.)

You also need to provide a type argument to the SomeObject part of the List. Here I've used a wildcard, which means the list can hold any type of SomeObject, but once we put a SomeObject in to the list we don't know what their original type argument was anymore:

List<SomeObject<?>> objectList = new ArrayList<SomeObject<?>>();

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'));

for (SomeObject<?> object : objectList) {
    ...;
}

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.