cannot be resolved to a type

I want to change the return type of this method to a class (ReturnContainer) that can hold the tupelo object in addition to another return value that I need to send back to the calling method.

I’ve never worked with this <T> concept in Java before so I don’t how to reconfigure this method to work the way I need it to.

public static <T extends LocationCapable> List<T> test(Class<T> incomingClass)
{
    List<TestTuple<T>> tupelo = new ArrayList<TestTuple<T>>();
    return tupelo;
}

When I try to change the code to the listing below, I get the error:

T cannot be resolved to a type

How can I have a return type of ReturnContainer but still allow the incomingClass to be a dynamic type?

public static ReturnContainer test(Class<T> incomingClass)
{
            List<TestTuple<T>> tupelo = new ArrayList<TestTuple<T>>();
            ReturnContainer rc = new ReturnContainer(tupelo, incomingClass);
    return rc;
}

Answer

You’re missing the type parameter:

public static <T> ReturnContainer test(Class<T> incomingClass)
{
    List<TestTuple<T>> tupelo = new ArrayList<TestTuple<T>>();
    ReturnContainer rc = new ReturnContainer(tupelo, incomingClass);
    return rc;
}

To my eyes, though, this looks weird. Shouldn’t ReturnContainer have a type parameter, too? In which case, you’d have

public static <T> ReturnContainer<T> test(Class<T> incomingClass)
{
    List<TestTuple<T>> tupelo = new ArrayList<TestTuple<T>>();
    ReturnContainer rc = new ReturnContainer<T>(tupelo, incomingClass);
    return rc;
}

Leave a Reply

Your email address will not be published. Required fields are marked *