0

According to the question, Create instance of generic type in Java?

At the time of writing ,the best answer is found to be...

private static class SomeContainer<E> { 
    E createContents(Class<E> clazz) { 
       return clazz.newInstance(); 
    }
 }

But the answer works only for this SomeContainer.createContents("hello");

My condition is to put the class as an inner class in a generic class,then the code should be like this.

SomeContainer<T>.createContents(T);

That will produce compile time error. There is not any created references for T,also. Is there any way to create completly new T object inside the generic class?

Thanks in advance

Community
  • 1
  • 1
johnkarka
  • 365
  • 2
  • 14
  • 2
    How is the type parameter `T` related to the type parameter of the enclosing generic class? It would be better if you show short version of your class. – Rohit Jain Aug 22 '13 at 18:35
  • 2
    Did you mean `SomeContainer.createContents(T.class);`? – ajb Aug 22 '13 at 18:49
  • A better idea, instead of `Class`, use `Class extends E>`; this way, someone can pass in an instance of a subclass's class, or use the `getClass()` method on an object (as it returns `Class extends |X|>` where `|X|` is the reference type of the object) without a cast. – gparyani Aug 22 '13 at 18:50
  • @ajb You can't use `.class` notation on a generic type parameter, because of type erasure. – gparyani Aug 22 '13 at 18:51
  • @gparyani OK. I really wasn't clear on whether T was supposed to be a generic parameter or just a dummy name he was using to ask his question. I think some actual code would be helpful. – ajb Aug 22 '13 at 19:00
  • Sorry friends. I misunderstood my problem. The comment form @gparyani can solve for problem. – johnkarka Aug 22 '13 at 19:05
  • What exactly I want is `T.class`.But because of type erasure I have to create new instance of `T` to be passed into `createContents(T)` method.Then my real question should be "Is it a common practice to create another new `T` to get such object type of `T`?" – johnkarka Aug 22 '13 at 19:14
  • In real Java world,is it common to create a new sample object to be passed into a generic class so that we get an new instance of such object from that generic class? – johnkarka Aug 22 '13 at 19:35
  • I want to know why "type erasure" is not terminated in Java. Is there any reason? – johnkarka Aug 22 '13 at 19:38
  • 1
    @John Runtime generics are on track for Java 10. The current version is Java 7, and Java 8 is on track for release in ~6 months as of writing. – gparyani Aug 22 '13 at 23:00

1 Answers1

1

Due to implementation of generics in Java you must pass the Class<T> object for the further use.

Here is the example:

public class Outer<E> {

    private static class Inner<E> {
        E createContents(Class<E> clazz) {
            try {
                return clazz.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                return null;
            }
        }
    }  

    private Class<E> clazz;
    private Inner<E> inner;

    public Outer(Class<E> clazz) {
        this.clazz = clazz;
        this.inner = new Inner<>();
    }


    public void doSomething() {
        E object = inner.createContents(clazz);
        System.out.println(object);
    }

}

Also you can use <? extends E>, proposed by @gparyani here

Community
  • 1
  • 1
Dany
  • 1,490
  • 1
  • 12
  • 22
  • Oh great! You are the one who really understands my problem. But it's confusing for me if `Inner inner =new Inner<>();` is valid and how it is different from `Inner inner = new Inner();` – johnkarka Mar 13 '14 at 06:38
  • @johnkarka Here is the diamond syntax introduced in Java 7, it's some kind of type inference, compiler will substitute type by itself. For Java 6 you should use `Inner inner = new Inner();`. – Dany Mar 13 '14 at 06:44
  • @johnkarka BTW mutlicatch will work starting from Java 7 too. – Dany Mar 13 '14 at 06:45
  • `multicatch` ? please explain – johnkarka Mar 13 '14 at 07:40
  • @johnkarka In Java 6 you can catch only one type of exceptions, so you must write catch-block for every type, you want to catch: `catch (IOException ex) { logger.log(ex); } catch (SQLException ex) { logger.log(ex); }` In Java 7 new syntax was introduced. The example above can be rewritten as: `catch (IOException|SQLException ex) { logger.log(ex); }` – Dany Mar 13 '14 at 16:32