The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

While this is functional, it seems bulky to fetch an item from map, and then it's class. Is there a better way to test against a known class without comparing to an existing actual instance of the class? (note this is not to compare the actual objects, but the class itself before acting on the incomingObject) ...maybe there is a better approach from the beginning

Re: Compare instance of a class to another

instanceof operator may be what you are looking for in the short term. Looks like it could be simplified using several different methods/patterns, the Template Pattern being the first that comes to mind, but you could use others or a combination of patterns (for example a Map valued with a class/interface that implements the behavior)

This example I found suggests that MY implementation will always be true because my test is to determine which child of the super was received. Thanks again for the input, while this did not directly solve the problem, it has placed me on the path with a light at the end.

I also found this page which may be useful to others reading this with similar questions.

Last edited by srs; November 26th, 2010 at 12:59 AM.
Reason: Link added

edit: better yet, you don't even need the name. Just use the class object returned from getClass() and compare that to the class you want (you'll have to first check for null objects, by definition null means the object is every type and no type all at the same time).

Re: Compare instance of a class to another

Originally Posted by helloworld922

edit: better yet...

if(child.getClass()== Parent.class)

So the short answer is use .class for the known, existing object?
So for my use it may look something like this:

if(incomingObject.getClass()== map[0][0].class()){// Note the null check is actually before here,// and this is the final check before the guts run// so by now its fairly well expected to match class// but it is known on rare occasion it can be one other class

and if so that leads me to ask what would be wrong with:

if(incomingObject.class== map[0][0].class){

...since by now the incoming/unknownObject is an instance of something

What is the difference in the two ways? .class vs .getClass()

As it turns out java.util.Iterator answered this problem by elimination, but the question remains.

Re: Compare instance of a class to another

You can only use the .class on actual type names, not variables. If you want to get the same class object from an actual object, use the getClass() method. Note that you will need to check to see if the object is null first since a method call on a null object will always fail.

// this will fail if either incommingObject or map[0][0] are null, you'll need to check for null before-handif(incomingObject.getClass()== map[0][0].getClass()){