This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes passing class name rather then object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "passing class name rather then object" Watch "passing class name rather then object" New topic
Author

passing class name rather then object

Darren Tweedale
Greenhorn

Joined: Jan 30, 2002
Posts: 16
Hello,
I have a small problem which I hope you folks can solve. Basically I have a class that has a vector of different type of objects (organisation, person, supplier, whatever). I want to be able to get an object by its CLASS NAME not by object.

as you can see, I am just adding an object to the X object using the method add.
X.add(new Organisation());
X.add(new Person());
now I want to get the object from the X class using the class name (not the object)
ie.
X.get(Organisation);
-- Organisation is the class - I want it to return me the object Organisation that was added to the vector
X.get(Person);
-- Person is the class - I want it to return me the object Person thats was added to the vector
Is this possible?
Any suggestions would be appricated.
Thanks
Darren Tweedale
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Darren,
Why are you double-posting? I'd recommend you back to the original post and edit it if you wanted to change the post. But this one should be deleted. You're just going to end up confusing alot of people, or annoying them.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Darren Tweedale
Greenhorn

Joined: Jan 30, 2002
Posts: 16
Sorry for confusing everyone! I have requested to delete my previous "incompleted" topic, but I think Nathaniel solved my problem with
object.class.getClass();
many thanks.
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Darren,
How's this?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
This is a puzzling requirement. What if an organization had a hundred people, so you have 101 objects in your Vector. Which one should you retrieve when you get("Person")?
Maybe we should be exploring richer data structures. Like a Vector for each classname and a Map to hold the Vectors. If you haven't run into Map yet, look at the API. It's a collection like Vector but you can get things out by a key like classname. So you could get the Vector of people using the key "Person"
All this talk of using classname is very dynamic. It will hold anything in the world, Oranizations, People, Toasters, Mini Coopers, etc. I wouldn't encourage this kind of generic storage thingy unless you really need to handle any kind of unforseen data.
If you have a model of a business with Organization and Person, maybe we should make a less dynamic object model of that particular structure.


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
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Stan,
Maybe you should try something simpler like using a separate collection for each of the things that you are interested in. Consider using a collection for your "Persons", another collection for your "OtherThings", and so on. Trying to cram everything into one container will just end up being a nightmare for you later down the road.
Darren Tweedale
Greenhorn

Joined: Jan 30, 2002
Posts: 16
Thanks for that Nathaniel - that was exactly how I did it - it worked a treat.
As for stan, I see what you mean, but I have discounted for that, as this vector will have only one organisation or one person at any one time - its part of a login in system. Lately, I had to put in a new entity (third-party=someone acting on their behalf) which this "third-party" can also be a person, an organisation or just on its own.
For example, I log in with my id, and I am a person as well as a third-party for someone else, therefore I should have two entities in the vector (person and third-party)
So I am just trying to be flexable by using classes for any future unknown entities.
Thanks once again for all help.
Darren
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: passing class name rather then object