Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7894743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:28:23+00:00 2026-06-03T07:28:23+00:00

There is need to compare two objects based on class they implement? When to

  • 0

There is need to compare two objects based on class they implement? When to compare using getClass() and when getClass().getName()?
Is there any difference between this approaches to compare two Objects class types (names)?

public abstract class Monster { ... }
public class MonsterTypeOne extends Monster { ... }
public class MonsterTypeTwo extends  Monster { ... }

    Monster monster = MonsterTypeOne();
    Monster nextMonster = MonsterTypeTwo();


if(nextMonster.getClass().getName().equals(monster.getClass().getName()) )// #1

if(nextMonster.getClass().equals(monster.getClass()) )// #2

EDIT 1


What about: ?

nextMonster.getClass().equals(MonsterTypeOne.class)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:28:25+00:00Added an answer on June 3, 2026 at 7:28 am

    Is there any difference between this approaches to compare two Objects class types (names)?

    Yes. Two classes may have the same name if they are loaded by different ClassLoaders.

    “The basics of Java class loaders” says

    At its simplest, a class loader creates a flat name space of class bodies that are referenced by a string name.

    “Eclipse – a tale of two VMs (and many classloaders)” says

    That means it’s possible to have two classes with the same name loaded into a VM at once, provided that they have two separate ClassLoaders


    When to compare using getClass() and when getClass().getName()?

    If you want to know whether two objects are of the same type you should use the equals method to compare the two classes — the first option.

    I can’t imagine why you’d want to do this, but if you want to know whether two objects with different concrete types have types with the same fully qualified name, then you could use the second. If you don’t understand “concrete types” and “fully qualified names” in the context of Java then you’re not writing type analysis code for java so you don’t want to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to compare two lists where each list contains about 60,000 objects. what
How do I compare two objects of a custom class in Objective-C? I try
in my code, I need to compare two strings to see if they are
Is there any need for code with author's name added in every function or
what I need: - there are two images: a background (large) and a proifile
Is there any need to use a custom buffer to either read or write
I need to compare two arrays. If I test if Object1==Object2 I am testing
This is all in C#, using .NET 2.0. I have two lists of objects.
I have two arrays I need to compare.... $Drink Array ( [0] => Drink
Is there a way in which we can equate two objects? I know that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.