How to create generic type?

Erik Z

I've defined a class like this:

public class MyClass<T implements MyInterface> {
    public T getMy() {
        return new T();
    }
}

This won't compile. I'm not allowed to create the generic type T.

How can I solve this? Is there any good patterns to do this? Can I solve this by using an abstract class instead of the interface? Do I have to use reflection?

aljipa

You can achieve this by passing an actual class type (e.g. to the constructor), Class<T> and calling newInstance() on it if default constructor is OK with you. If you need another constructor you would need to use reflection API, e.g. via getDeclaredConstructors() on this very class type object.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create a generic typesafe HashMap by class type?

From Dev

How to create generic type reference with Roslyn?

From Dev

how to create an alias to a complicated generic type in Java?

From Dev

How to create object/singleton of generic type in Scala?

From Dev

How to create a function that returns a generic protocol type?

From Dev

How to create a custom deserializer in Jackson for a generic type?

From Dev

How to create a type safe generic array in java?

From Dev

How to create Generic class that takes unlimited type

From Dev

How do I create a Factory for a generic type?

From Dev

How do I create a Factory for a generic type?

From Dev

How to create type of func with specified generic types?

From Dev

How to create an instance just by generic type T

From Dev

How do I create an instance of a generic type?

From Dev

How do I create a Type with multiple generic type parameters

From Dev

How to create an enumeration that accepts a generic type as associated type

From Dev

how to create a collection of a type that has a generic value type argument

From Dev

Create a map with a generic type

From Dev

Create a Function with Generic Type

From Dev

How do I create a generic class that takes as its generic type a generic class?

From Dev

How can I create a generic collection which contains a specific type?

From Dev

How do I create new instance of Generic Type Class

From Dev

How to create instance of a generic C# type in F#?

From Dev

How to create an Action<T> with Reflection where T is a discovered generic Type

From Dev

How can create a generic type that is a combination of other types in TypeScript?

From Dev

How to create a fixed size array of a generic type in Java?

From Dev

How to create instance of a generic C# type in F#?

From Dev

Create an enum set of generic type

From Dev

Create a generic type parameterized by a number

From Dev

Create an instance of a generic type in Scala?

Related Related

  1. 1

    How to create a generic typesafe HashMap by class type?

  2. 2

    How to create generic type reference with Roslyn?

  3. 3

    how to create an alias to a complicated generic type in Java?

  4. 4

    How to create object/singleton of generic type in Scala?

  5. 5

    How to create a function that returns a generic protocol type?

  6. 6

    How to create a custom deserializer in Jackson for a generic type?

  7. 7

    How to create a type safe generic array in java?

  8. 8

    How to create Generic class that takes unlimited type

  9. 9

    How do I create a Factory for a generic type?

  10. 10

    How do I create a Factory for a generic type?

  11. 11

    How to create type of func with specified generic types?

  12. 12

    How to create an instance just by generic type T

  13. 13

    How do I create an instance of a generic type?

  14. 14

    How do I create a Type with multiple generic type parameters

  15. 15

    How to create an enumeration that accepts a generic type as associated type

  16. 16

    how to create a collection of a type that has a generic value type argument

  17. 17

    Create a map with a generic type

  18. 18

    Create a Function with Generic Type

  19. 19

    How do I create a generic class that takes as its generic type a generic class?

  20. 20

    How can I create a generic collection which contains a specific type?

  21. 21

    How do I create new instance of Generic Type Class

  22. 22

    How to create instance of a generic C# type in F#?

  23. 23

    How to create an Action<T> with Reflection where T is a discovered generic Type

  24. 24

    How can create a generic type that is a combination of other types in TypeScript?

  25. 25

    How to create a fixed size array of a generic type in Java?

  26. 26

    How to create instance of a generic C# type in F#?

  27. 27

    Create an enum set of generic type

  28. 28

    Create a generic type parameterized by a number

  29. 29

    Create an instance of a generic type in Scala?

HotTag

Archive