Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (482)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (550)
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 1353 times)
0 Members and 1 Guest are viewing this topic.
Offline Jamison

Junior Member




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 Kernel


Medals: 153
Projects: 23
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 Member




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 Kernel


Medals: 153
Projects: 23
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 Member




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 Kernel


Medals: 153
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


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

The values for your arguments

Kev

Avenger
Guest
« Reply #6 - Posted 2006-04-04 10:10:29 »

Hey thanks ! I did not know that either.So  let's say the Class's constructor  needs 2 BufferedImages we will do it like that?
Class.forName(name).getContructors()[0].newInstance(image1 , image2);   ?

Where ofcourse image1 and image2 are 2 BufferedImages?

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2006-04-04 10:20:17 »

new Object[]{image1, image2}

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Jamison

Junior Member




We're all idiots in one way or another.


« Reply #8 - Posted 2006-04-04 18:23:25 »

Ahh thanks for the clarafication Riven. So say I had a Class that required two Strings, this would suffice:
1  
2  
3  
4  
5  
6  
Object ob = new Object() {
   String a = "String A";
   String b = "String B";
};

Class.forName("Test").getContructors()[0].newInstance(ob);

Assuming the arguments are named a and b.

I don't like you. Check out my site Smiley www.gamedevforums.com
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2006-04-04 18:35:57 »

I'd suggest you'd not only ask, but also try to compile it and see what error-messages are reported, because the above code will never compile.

Object[] arr = new Object[]{ "String A", "String B" };

or

Object[] arr = new Object[2];
arr[0] = "String A";
arr[1] = "String B";

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jamison

Junior Member




We're all idiots in one way or another.


« Reply #10 - Posted 2006-04-04 19:30:19 »

Ahh okay I see now. Sorry about that.

I don't like you. Check out my site Smiley www.gamedevforums.com
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (14 views)
2014-08-22 19:31:30

atombrot (28 views)
2014-08-19 09:29:53

Tekkerue (25 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (15 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (61 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!