Java: creating objects of arrays with different names at runtime and accessing/updating them
Posted
by scriptingalias
on Stack Overflow
See other posts from Stack Overflow
or by scriptingalias
Published on 2010-04-03T03:28:38Z
Indexed on
2010/04/03
3:33 UTC
Read the original article
Hit count: 259
java
Hello,
I'm trying to create a class that can instantiate arrays at runtime by giving each array a "name" created by the createtempobjectname() method. I'm having trouble making this program run. I would also like to see how I could access specific objects that were created during runtime and accessing those arrays by either changing value or accessing them. This is my mess so far, which compiles but gets a runtime exception.
import java.lang.reflect.Array;
public class arrays { private static String temp; public static int name = 0; public static Object o; public static Class c;
public static void main(String... args) { assignobjectname(); //getclassname();//this is supposed to get the name of the object and somehow //allow the arrays to become updated using more code? } public static void getclassname() { String s = c.getName(); System.out.println(s); } public static void assignobjectname()//this creates the object by the name returned { //createtempobjectname() try { String object = createtempobjectname(); c = Class.forName(object); o = Array.newInstance(c, 20); } catch (ClassNotFoundException exception) { exception.printStackTrace(); } } public static String createtempobjectname() { name++; temp = Integer.toString(name); return temp; }
}
© Stack Overflow or respective owner