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 8704795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:12:01+00:00 2026-06-13T03:12:01+00:00

I am making a method .equals replacing the equals method used. It accepts a

  • 0

I am making a method .equals replacing the equals method used. It accepts a object. I want it to check if that object equals the class that runs the .equals class.

I know I want to compare all the private methods I have to that object.

Is there a way to do this without making another private class to get the private variables from the object? How do I do this to compare equality not identity? I am stuck on this. Do i have to use == to compare?

Also looking online i see others use recursion. If this is the way i have to do it can you show and explain it to me?

so an example i have

public boolean equals(Object o)
{

this is in a class we will call bobtheBuilder (first thing to pop in my head)
I want to check if the object o is equal to the class.
bobTheBuilder has private object array and a private int. I assume I want to check if the array and int of this class equal the array and int of the object. First I know i need to check if it equals null so

private _arrayObject[];

private _integerBob;

public boolean equals(Object o)
{
    boolean result = true;

    if (o == null)
        result = false

    bobTheBuilder compared = (bobTheBuilder) o;

    if(compared.getArray != _arrayObject[] || compared.getInt != _integerBob)
        result == false

    return result
}

I know that equal checks for equality not identity so I assume this is wrong. How would I do this

  • 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-13T03:12:02+00:00Added an answer on June 13, 2026 at 3:12 am

    Adapted from the examples in Effective Java, 2nd edition by Joshua Bloch:

    public boolean equals(Object o) {
      if (o == this) {
        return true;
      }
    
      if (!(o instanceof BobTheBuilder)) {
        return false:
      }
    
      BobTheBuilder bob = (BobTheBuilder) o; //guaranteed to succeed
    
      return Arrays.equals(bob._arrayObject, _arrayObject)
        && bob._integerBob == _integerBob;
    }
    

    Don’t forget to override hashCode as well.

    The text in the book refers to the contract set out for the equals method in Object which is here

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

Sidebar

Related Questions

I am making a base class that has a virtual method called GetBaseAddresses(). It
So I was making a class the other day and used Eclipse's method to
I am attempting to making a method that on a button click, it will
I want to use the class information that was captured by the setup of
I'm making a linked list class and am trying to implement this contains() method.
I was having a look at the Equals method implementation of the String class
I have a class that uses XML and reflection to return Object s to
I understand that only one instance of any object according to .equals() is allowed
I'm making a method that load a graph from a file. It's very simple,
I'm making a class which parses through all the chars in a file and

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.