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

  • SEARCH
  • Home
  • 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 8441877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:41:18+00:00 2026-06-10T08:41:18+00:00

I have one class and I want compare two objects of that class. Is

  • 0

I have one class and I want compare two objects of that class. Is there any method to
compare two user defined objects. I don’t want to do it manually.

public class NewClass {

    private String name;
    private String surnamename;
    private String address;

    public NewClass(String name)
    {
        this.name = name;
    }

    public NewClass(String name, String surname, String address)
    {
        this.name = name;
        this.surnamename = surname;
        this.address = address;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSurnamename() {
        return surnamename;
    }
    public void setSurnamename(String surnamename) {
        this.surnamename = surnamename;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}

I want to compare two objects of this class. how could I accomplish this?

NewClass newClass1 = new NewClass("kshitij", "solanki", "nadiad");      
NewClass newClass2 = new NewClass("kshitij", "solanki", "nadiad");

    String s1 = new String("kshitij");
    String s2 = new String("kshitij");

    NewClass s3 = new NewClass("kshitij");
    NewClass s4 = new NewClass("kshitij");

    System.out.println(s1.hashCode());
    System.out.println(s2.hashCode());

    System.out.println(s3.hashCode());
    System.out.println(s4.hashCode());

    System.out.println(newClass1 == newClass2);
    System.out.println(newClass1.toString() == newClass2.toString());
    System.out.println(newClass1.toString().equals(newClass2.toString()));
  • 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-10T08:41:20+00:00Added an answer on June 10, 2026 at 8:41 am

    You want to implement equals and hashCode, e.g.

    public boolean equals(final Object other) {
      if (other instanceof NewClass) {
        final NewClass _other = (NewClass) other;
        return name.equals(_other.name)
          && (surname == null && surname == _other.surname
            || surname.equals(_other.surname))
          && (address == null && address == _other.address
            || address.equals(_other.address));
      } else {
        return false;
      }
    }
    
    public int hashCode() {
      return name.hashCode() * 961
        + (surname == null ? 0 : surname.hashCode()) * 31
        + (address == null ? 0 : address.hashCode());
    }
    

    Now, you can compare NewClass instances by their semantics, e.g.

    /* they are equal */
    assert new NewClass("A", "B", "C").equals(new NewClass("A", "B", "C"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Java. I want to have a setter method of one class that
I have a Generic Base Class that I want to allow one of two
I have a class that instantiates two classes which implement interfaces. I want one
I have a class that is mapped in fluent nhibernate but I want one
I want to compare C++ class/structure objects. In C, most of the time, one
I have a method to compare two list of objects. The objects are unique
I want to call a method where two parametres are objects (one retrieve Person,
I have one abstract class -let's say myBase. And I want all the classes
In my app i have created one custom class for Label.Now i want to
I have one class called Global and two other activities. In each activity I

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.