*
The moose likes Beginning Java and the fly likes naming objects dynamically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "naming objects dynamically" Watch "naming objects dynamically" New topic
Author

naming objects dynamically

kasey mccurdy
Greenhorn

Joined: Nov 26, 2004
Posts: 7
howdy howdy fellow java geeks...

im an extreme N00B when it comes to java, but im decent with other SIMILAR languages...

heres my problem:

i need to basically 'name' objects 'dynamically'...but for some reason, it just isnt working...here's my code...can anyone tell me why this wont work?



the "employeeCount" variable is an auto incrementing variable, that i was HOPING to use to create a unique instance name for the object...can this be done? or are all the instantiated objects stored in an array and only accessable through that array?

thank you so much in advance!

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
When you create an Employee object and want to keep a reference, you'll have to assign that reference to a variable that is declared as Employee or some superclass or interface. So you could try any of these:

What's common to each of those is that we putting a reference to the new object into a variable declared to handle it. Now, you want to keep a bunch of employee objects for various employees and be able to get them back later. After getting our reference we could call methods something like this:

Take a look at the JavaDoc for Map. Maps let us store object instances with some kind of key. I used empId as the key. I can put an object in for id "123" and get it back later. The syntax you showed - which isn't quite Java syntax - was probably trying to imply something much like that.

That threw a lot of new stuff out there all at once. Any of it makes sense? Or not? If you have a specific programming task, take a shot at it, post some code here and we'll see if we can guide you along. Keep having fun!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Since Java is a compiled language, all variable names must be resolved at compile-time. This may seem strange if you have experience with an interpreted language like Perl where you can build the variable name from the values of other variables. However, Java provides enough other tools that this usually isn't a deficiency. For instance, since you are using a counter in attempting to create the "dynamic variable name", this suggests that an array would be very suitable. Stan's first example above includes an array, but if you are unfamiliar with arrays, then you should google for the syntax and other details. If you need a more sophisticated system to look up a value, rather than just by an integer index, then you should look at the Map class, as Stan suggested. However, I suspect this may be something to just remember for another day as it seems to me that an array will do just fine in this situation.

Keep Coding!

Layne


Java API Documentation
The Java Tutorial
kasey mccurdy
Greenhorn

Joined: Nov 26, 2004
Posts: 7
First off...

YOU TWO ARE THE SH1T.

thank you so much for helping a noob such as myself, and Layne, you were right, i am very used to Actionsript/ASP/PHP where i can create references to objects and whatnot using variable names at runtime...i understand now why i cant do that!

and Stan, thanks for the array solution, worked like a charm...i was wondering if java didnt automatically put all objects into some sort of accessable array, but putting them in my OWN array is much smarter...just never knew how to do this!

thank you so much! PROBLEM RESOLVED!
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Originally posted by kasey mccurdy:
First off...

YOU TWO ARE THE SH1T.


Isn't that usually an insult???


42
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Kids today and their hip lingo. Ya never know. Shiznit is a great accolade, originating from "that's good shiznit!"
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
thanks. Maybe I'm getting old...
If we said things like that in class we'd be sent to the head master and given punishment in the form of cleaning the bathrooms or sweeping the school yard after hours for several days...
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Stan James:
Kids today and their hip lingo. Ya never know. Shiznit is a great accolade, originating from "that's good shiznit!"

Well, I'm not exactly a kid any more. In fact, I've taken a LONG time to finish my Bachelor's degree. Apparently I'm still young enough to understand the lingo, though!

Layne
[ February 06, 2005: Message edited by: Layne Lund ]
kasey mccurdy
Greenhorn

Joined: Nov 26, 2004
Posts: 7
im actually 23..so dont know if that classifies me as a kid...
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Well, I'm nearly twice that, so you're not too old for me to muss your hair when you spout that modern slang I see you started this at 5 posts. If we missed the first one, welcome to the ranch! Keep coding, keep having fun.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: naming objects dynamically
 
Similar Threads
Why arrays are object ?
I need help for algorithrm
Capturing a list of details from the UI to ActionForm
Retriving data from vector by matching user id
what is the error in this program