I am looking to make my code more abstract so I dont need to repeat the same code. I am storing objects two different classes in the same arraylist. When searching, I need to check ahead of time to see if the entry in question is of a certain class before I compare any other values.

I've looked around online and all the comparison methods seem to be comparing the values inside the objects instead of comparing the class of each object.

Any predefined methods out there?

7 Years
Discussion Span
Last Post by BLUEC0RE

I should add that I want to compare one object to another, not comparing an object to an actual class definition (such as instanceof, which compares an object to a class def.)


I have to admit that I am a little bit confused - if you want to check the type of the object, you can use instanceof. If you want to compare two objects of the same type you can override the equals method. I am not sure what you are trying to do...


yeah I have a habit of making confusing posts :S.

I have three objects (ob1, ob2, ob3)

ob1 = class1
ob2 = class2
ob3 = class1

ob1.equalsClass(ob2) = false
ob1.equalsClass(ob3) = true

the values inside dont matter, just as long as the objects classes are equal

Edited by BLUEC0RE: n/a


I see, then use the getClass() and getName() methods to get the name of the class as a String, for example:

Integer var = new Integer();

Will return "java.lang.Integer". After you have that, it's a question of comparing Strings.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.