jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes polyNOOBisim Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "polyNOOBisim" Watch "polyNOOBisim" New topic


scott beveridge

Joined: Mar 03, 2005
Posts: 11
Howdy pardners,

Can someone please tell me how to reference objects through the type or value of their variables, rather than the actual reference.

This is a program idea I had that prompted me to start learning, so I don't have any code yet, but essentially I want to say this in Javanese:

PERFORM {operation} ON (object with variable of type X)
PERFORM {operation} ON (object with X as the value for any variable).

My thinking at the moment is to put everything in an array then loop through it and use exceptions. Or design the classes in some fiendish manner which currently eludes me.

Please tell me the easy answer or point me to where I can find it.

Thanks, you guys have helped me a lot already.

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Hi Scott,

Welcome to JavaRanch!

Your ideas are basically correct. If the objects are all of the same type, then instead of using an array, I might use multiple Maps (like java.util.HashMap). Looking things up in a Map is both simpler and faster than searching linearly through an array. You could use one map for each variable, and use the value of the variable as the key for an object.

If you want to do this for many different kinds of objects, then the best thing to do (conceptually) would be to put the objects into a database, with a table like

Then you can find the objects of interest using a database query. There are lots of different databases to choose from, including library-based in-memory ones and free software ones.

Just thinking out loud; hope this helps.

[Jess in Action][AskingGoodQuestions]
scott beveridge

Joined: Mar 03, 2005
Posts: 11
Thank you Ernest,

And yes it does help very much. Next on the learning curve is hashmaps. The objects all have the same base class so I may adjust things to make it easier. Its just a matter of working out exactly what has to happen.

I was intending to use a database for something else, but after re-reading your post a few times you gave me an idea.
Can multiple maps be used to store a reference to code that will be chosen from at run-time? Like maybe handed to a new thread while the main one is halted to wait for the return? Even if the reference was just to a file location containing the code.
With something like that I could avoid using (learning about) a database entirely and store everything in a seperate file.

Then again the size could run to a few hundred entries, is that an acceptable overhead for the map?

My turn to think out loud.

I agree. Here's the link: http://aspose.com/file-tools
subject: polyNOOBisim
It's not a secret anymore!