new array using reflection

Posted on 2006-11-09
Last Modified: 2012-06-21
Java generics don't allow me to do something like:

public static <T> void doStuff(T obj) {
    T[] arr = new T[5];
    // ...

It won't let me create the array of type T.

So, is it possible to create a new array of type T using reflection? Is there a method like to
which will create an array of T?
Question by:HappyEngineer
LVL 13

Expert Comment

ID: 17911907
u can use Object, which is parent of all java classes for generalize a data type

public static Object void doStuff(Object obj) {
    Object []arr = new Object[5];
    // ...

when u need to convert
String str = (String)obj; //convert into String
Integer i = (Integer)obj; //convert into Integer
but u have to make sure, that the object is actually belongs to String/Integer class

LVL 92

Expert Comment

ID: 17911989
Object array = Array.newInstance(T.class, 10);

Author Comment

ID: 17912073
objects, that almost works, but T.class apparently isn't legal.

It says: "Illegal class literal for the type parameter T"
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 92

Expert Comment

ID: 17912092
sorry, thought T was a class name, not a parameter.
LVL 92

Accepted Solution

objects earned 250 total points
ID: 17912104
what about:

Object array = Array.newInstance(obj.getClass(), 10);
LVL 23

Assisted Solution

Ajay-Singh earned 250 total points
ID: 17912182
try this:

T[] arr = (T[]) new Object[5];

Author Comment

ID: 17973888
The casing of the array works at compile time but breaks at run time.

I can't call obj.getClass() because in this particular case I don't actually have an object to call getClass() on.

Never mind, I've worked around it. Thanks anyway!

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 123
JAVA API design with micro service cloud in mind 1 75
swing controls 2 21
Java 8 to Java 6 8 32
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question