Brian Antiqueña
Brian Antiqueña

Reputation: 188

Java, create an instance by only using the passed parameter generic type

private static <T> String mapQueryResults(T objNew) {
    T obj = new T();
    ...
}

It gives me error "Cannot instantiate the type T". How can I create an instance? Thanks

Upvotes: 0

Views: 69

Answers (3)

hfontanez
hfontanez

Reputation: 6168

When topics like these come up, my approach is "forget programming and think logically."

The OP asked

create an instance by only using the passed parameter generic type

Basically, you want a constructor to build a concrete thing based on something generic. Is this possible in real life? Say that you need "something" built and you go to a builder and you tell the builder "I want you to build something for me" and provide no context, no additional information. Is it realistic to expect that the builder will be able to build what you need? Of course the answer is no. Unless you specify in sufficient detail a description of what you want, the builder is not going to know what to build.

T or "type", as others already mentioned, could be anything. If this was allowed by the language, you will most likely end up with nothing usable. It would be like having T be dough. Unless you specifically tell someone you want pizza, or bread, or flour tortillas, there is no way you can expect someone to make what you want if the only thing that is known is "dough."

Different "types" have different requirements for construction. In fact, some "types" cannot be built at all (they might have a private constructor). Others the constructor is hidden behind static factory methods. In short, there are too many reasons why this is simply not possible.

All that said, you are not really wrong thinking that there should be a way where you can pass a set of different "types" of parameters to some function and end up with different kinds of concrete objects built. If that is what you are thinking, then what you need is a Design Pattern (or patterns) that will support this notion.

In the original Gang of Four book, you will find 5 CREATIONAL Design Patterns:

  1. Abstract Factory - factory that builds family of things (i.e. a factory of factories)
  2. Builder - Builds the object in stages (typically when objects are too complex)
  3. Factory Method - factory that build single type of object (or different things based on a common abstraction)
  4. Prototype - Creates a new object based on another object
  5. Singleton - Ensures a single instance is created

Based on the little information in the original post, I believe the OP needs to implement either a Factory Method or possibly a Prototype pattern.

Upvotes: 1

boot-and-bonnet
boot-and-bonnet

Reputation: 761

You could change the method argument to accept a Supplier<T> constructor function, and pass in something like MyClass::new:

public class MyClass {
    public MyClass() {}
    public MyClass(String s) { ... }
}

public static void main(String[] args) {
    mapQueryResults(MyClass::new);
    mapQueryResults(() -> new MyClass("test"));
}   
    
private static <T> String mapQueryResults(Supplier<T> objNew) {
    T obj = objNew.get();
    ...
}

Upvotes: 0

Pieter12345
Pieter12345

Reputation: 1789

You cannot do this. Type T could be any type, including interfaces and abstract classes, which cannot be instantiated themselves. Even if a non-abstract class is passed, the empty constructor might not exist. If you wish to pass something to be able to create new instances of some class, then you could pass some factory class that creates these new instances for you through some method.

Upvotes: 1

Related Questions