10

We all know we can create an object with the help of class name in the string format. Like i have a class name "Test". Using

Class.forName("Test").newInstance()

We can create the object of that class.

My question is that is there any way to create an array or array list of the objects using class name ?? OR lets suppose we have an object of the class and can with this object we create the array or array list of the that object.

3
  • Did you mean "array of array list" or "array or array list"? Commented Jun 14, 2013 at 9:21
  • ArrayList is easy: new ArrayList<?>() Commented Jun 14, 2013 at 9:22
  • @jon array or array list Commented Jun 14, 2013 at 9:23

2 Answers 2

18

To create an array, you can use java.lang.reflect.Array and its newInstance method:

Object array = Array.newInstance(componentType, length);

Note that the return type is just Object because there's no way of expressing that it returns an array of the right type, other than by making it a generic method... which typically you don't want it to be. (You certainly don't in your case.) Even then it wouldn't cope if you passed in int.class.

Sample code:

import java.lang.reflect.*;

public class Test {
    public static void main(String[] args) throws Exception {
        Object array = Array.newInstance(String.class, 10);

        // This would fail if it weren't really a string array
        String[] afterCasting = (String[]) array;
        System.out.println(afterCasting.length);
    }
}

For ArrayList, there's no such concept really - type erasure means that an ArrayList doesn't really know its component type, so you can create any ArrayList. For example:

Object objectList = new ArrayList<Object>();
Object stringList = new ArrayList<String>();

After creation, those two objects are indistinguishable in terms of their types.

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

5 Comments

Maybe I get the OP's question wrong, but I interpret it as "I want an array or arraylist of the type I created", so Object[] array = new Object[length] would do.
@Heuster: Well, that creates an Object[], which isn't "an array of the type I created". It's an array which can hold references of the type he created, but that's a different matter.
@jon "Object array = Array.newInstance(componentType, length);" This give me the object of array but i want to get object of arraylist. There is any way i can get this??
@WaqasAli: Did you read the end of the answer, which talks about ArrayList? I think you need to read up on type erasure.
@MvG: That doesn't compile. (I tried.) You can use it in a variable declaration, but not as part of instantiation.
6

You can use Array

Object xyz = Array.newInstance(Class.forName(className), 10);

It has a method newInstance(Class, int):

Creates a new array with the specified component type and length. Invoking this method is equivalent to creating an array as follows:

 int[] x = {length};
 Array.newInstance(componentType, x);

4 Comments

Array.newInstance return an object not the array of object
@WaqasAli: It does create an array, but that can't be expressed in the method signature. What would you expect the signature of Array.newInstance to be?
@JonSkeet since Java has covariant types, I guess Object[] would be a valid return type.
@JanDvorak: Nope, not if you pass in int.class.

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.