?
Solved

new array using reflection

Posted on 2006-11-09
7
Medium Priority
?
360 Views
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
    T.class.newInstance()
which will create an array of T?
0
Comment
Question by:HappyEngineer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 13

Expert Comment

by:marchent
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

~marchent~
0
 
LVL 92

Expert Comment

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

Author Comment

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

It says: "Illegal class literal for the type parameter T"
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 92

Expert Comment

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

Accepted Solution

by:
objects earned 750 total points
ID: 17912104
what about:

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

Assisted Solution

by:Ajay-Singh
Ajay-Singh earned 750 total points
ID: 17912182
try this:


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

Author Comment

by:HappyEngineer
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!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month10 days, 6 hours left to enroll

765 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