Solved

new array using reflection

Posted on 2006-11-09
7
352 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
7 Comments
 
LVL 13

Expert Comment

by:marchent
Comment Utility
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
Comment Utility
Object array = Array.newInstance(T.class, 10);
0
 

Author Comment

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

It says: "Illegal class literal for the type parameter T"
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 92

Expert Comment

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

Accepted Solution

by:
objects earned 250 total points
Comment Utility
what about:

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

Assisted Solution

by:Ajay-Singh
Ajay-Singh earned 250 total points
Comment Utility
try this:


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

Author Comment

by:HappyEngineer
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now