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?
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
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.
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!
Kids today and their hip lingo. Ya never know. Shiznit is a great accolade, originating from "that's good shiznit!"
Joined: Oct 12, 2000
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...
Joined: Dec 06, 2001
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!
im actually 23..so dont know if that classifies me as a kid...
Joined: Jan 29, 2003
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.