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?

Recommended Answers

All 6 Replies

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

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();
var.getClass().getName();

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

Perhaps this will work?

if (ob1.getClass() == ob2.getClass())
   ...

thanks a lot for getting back to me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.