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

10
  • 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. Commented Aug 22, 2013 at 18:35
  • 2
    Did you mean SomeContainer<T>.createContents(T.class);? Commented Aug 22, 2013 at 18:49
  • A better idea, instead of Class<E>, 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. Commented Aug 22, 2013 at 18:50
  • @ajb You can't use .class notation on a generic type parameter, because of type erasure. Commented Aug 22, 2013 at 18:51
  • 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. Commented Aug 22, 2013 at 23:00

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

Oh great! You are the one who really understands my problem. But it's confusing for me if Inner<E> inner =new Inner<>(); is valid and how it is different from Inner<E> inner = new Inner<E>();
@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<E> inner = new Inner<E>();.
@johnkarka BTW mutlicatch will work starting from Java 7 too.
multicatch ? please explain
@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); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.