Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store class generic in field in Java

Tags:

java

generics

Is there any way to store the generic parameter type passed in at construction to a parameter. My goal:

class generic<T> {
    Class<T> type;
    public generic() {
        super();
        this.type = //Something that gets class from T
    }
}

What I'm currently doing is this:

class generic<T> {
    Class<T> type;
    public generic(Class<T> type) {
        super();
        this.type = type;
    }
}

It seems silly to have to specify the class twice, but I'm not sure of how else to do it. I think this might be possible with reflection, but I haven't investigated that yet. Is there a more straightforward way? If not (as an aside) why the information loss?

like image 796
David Berger Avatar asked May 26 '09 16:05

David Berger


People also ask

Can we inherit generic class in Java?

It inherits all members defined by super-class and adds its own, unique elements. These uses extends as a keyword to do so. Sometimes generic class acts like super-class or subclass. In Generic Hierarchy, All sub-classes move up any of the parameter types that are essential by super-class of generic in the hierarchy.

How do you pass a generic Object in Java?

Generics Work Only with Reference Types: When we declare an instance of a generic type, the type argument passed to the type parameter must be a reference type. We cannot use primitive data types like int, char. Test<int> obj = new Test<int>(20);

How do you make a class generic in Java?

A Generic Version of the Box Class To update the Box class to use generics, you create a generic type declaration by changing the code "public class Box" to "public class Box<T>". This introduces the type variable, T, that can be used anywhere inside the class.

How can you bound generic types?

Whenever you want to restrict the type parameter to subtypes of a particular class you can use the bounded type parameter. If you just specify a type (class) as bounded parameter, only sub types of that particular class are accepted by the current generic class. These are known as bounded-types in generics in Java.


2 Answers

Because Java generics are implemented with Type Erasure

When a generic type is instantiated, the compiler translates those types by a technique called type erasure — a process where the compiler removes all information related to type parameters and type arguments within a class or method. Type erasure enables Java applications that use generics to maintain binary compatibility with Java libraries and applications that were created before generics.

like image 60
bruno conde Avatar answered Oct 19 '22 15:10

bruno conde


If you use a static creation method with type inference instead of the constructor, then the type does not need to be specified twice.

final class Generic<T> {
    private final Class<T> type;
    private Generic(Class<T> type) {
        super();
        if (type == null) {
            throw new NullPointerException();
        }
        this.type = type;
    }
    public static <T> Generic<T> newInstance(Class<T> clazz) {
        return new Generic<T>(clazz);
    }
}
...
    someFunction(Generic.newInstance(SomeType.class));

Of course, if you want to store the result in a variable, you are probably going to repeat the type anyway!

like image 36
Tom Hawtin - tackline Avatar answered Oct 19 '22 16:10

Tom Hawtin - tackline