Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (525)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (594)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Creating an object from a variable  (Read 1452 times)
0 Members and 1 Guest are viewing this topic.
Offline Jamison

Junior Devvie




We're all idiots in one way or another.


« Posted 2006-04-03 22:52:20 »

Hello all,

I'm just wondering if it's possible to create an object from a variable String. For example, I have four classes named Tile1, Tile2, ..., etc. Now say I have a String, from which an ID character (1 - 4) is incremented into the String, could I create a class-object from that String? Like so:
1  
2  
3  
char id = '2'; // This character is arbitrary in my actual program, but just initiated as '2' for this example.
String name = "Tile"+id;
Tile t = new name(); // All Tile1...4 classes extend the main class, Tile.

Of course, the code above doesn't work because the compiler thinks I'm trying to create an object of class name, which doesn't exist. But is there any possible way to create an object of a class from a String, like above?

I don't like you. Check out my site Smiley www.gamedevforums.com
Offline kevglass

« JGO Spiffy Duke »


Medals: 210
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2006-04-03 23:03:17 »

Class.forName(name).newInstance();

Kev

Offline Jamison

Junior Devvie




We're all idiots in one way or another.


« Reply #2 - Posted 2006-04-03 23:13:20 »

Thanks a lot, kevglass! However, I'm getting InstantiationException:
1  
2  
3  
4  
5  
java.lang.InstantiationException: Tile1
        at java.lang.Class.newInstance0(Class.java:281)
        at java.lang.Class.newInstance(Class.java:249)
        at Tiles.<init>(Tiles.java:34)
        at Tiles.main(Tiles.java:88)

I'm assuming this happens since both my main Tile class and all of my Tile1...4 classes require parameters. Is there anyway to pass parameters to the newly created Object too?

EDIT: Yup, I get an error because my classes require parameters (I tested it by removing the parameter requirements of my classes). I guess I can just use a separate method, and after the Object is initiated, call that method.

I don't like you. Check out my site Smiley www.gamedevforums.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kevglass

« JGO Spiffy Duke »


Medals: 210
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #3 - Posted 2006-04-03 23:19:46 »

Yeah, it just gets a bit more complicated, you really want to have a lot at the Java Reflection API.

In this case you'd do something like:

1  
Class.forName(name).getContructors()[0].newInstance(args);


Kev

Offline Jamison

Junior Devvie




We're all idiots in one way or another.


« Reply #4 - Posted 2006-04-03 23:37:13 »

Hmm, according to the constructor of this:
1  
Class.forName(name).getContructors()[0].newInstance(args);

An Object is required. What am I supposed to pass to that?

I don't like you. Check out my site Smiley www.gamedevforums.com
Offline kevglass

« JGO Spiffy Duke »


Medals: 210
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #5 - Posted 2006-04-04 00:05:11 »

The values for your arguments

Kev

8: Undefined index: online
File: /home/jgo/public_html/Themes/default/Display.template.php (main sub template - eval?)
Line: 161