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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:09:35+00:00 2026-05-10T22:09:35+00:00

When running FindBugs on my project, I got a few instances of the error

  • 0

When running FindBugs on my project, I got a few instances of the error described above.

Namely, my overriding versions of equals cast the RHS object into the same type as the object in which the overriding version is defined.

However, I’m not sure whether a better design is possible, since AFAIK Java does not allow variance in method parameters, so it is not possible to define any other type for the equals parameter.

Am I doing something very wrong, or is FindBugs too eager?

A different way to phrase this question is: what is the correct behavior if the object passed to equals is not the same type as an LHS: Is this a false, or should there be an exception?

For example:

public boolean equals(Object rhs) {     MyType rhsMyType = (MyType)rhs; // Should throw exception     if(this.field1().equals(rhsMyType.field1())... // Or whatever } 
  • 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. 2026-05-10T22:09:36+00:00Added an answer on May 10, 2026 at 10:09 pm

    Typically, when implementing equals you can check to see whether the class of the argument is equal (or compatible) to the implementing class before casting it. Something like this:

    if (getClass() != obj.getClass())     return false; MyObj myObj = (MyObj) obj; 

    Doing it this way will prevent the FindBugs warning.

    A side note to address a comment:
    Some people argue to use instanceof instead of getClass to check type safety. There is a big debate on that, which I was trying not to get into when I noted that you can check for class equality or compatibility, but I guess I can’t escape it. It boils down to this – if you use instanceof you can support equality between instances of a class and instances of its subclass, but you risk breaking the symmetric contract of equals. Generally I would recommend not to use instanceof unless you know you need it and you know what you are doing. For more information see:

    • http://www.artima.com/weblogs/viewpost.jsp?thread=4744
    • What issues should be considered when overriding equals and hashCode in Java?
    • http://www.macchiato.com/columns/Durable5.html
    • http://commons.apache.org/lang/api-release/org/apache/commons/lang/builder/EqualsBuilder.html (Apache common’s implementation helper)
    • http://www.eclipsezone.com/eclipse/forums/t92613.rhtml (Eclipse’s default equals generator)
    • NetBeans generator also uses getClass()
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am running FindBugs on my project and am seeing a few instances where
Running the following line in my script gives me a Permission denied error: $config_out
Running SQL Server 2008 (not R2). I have a few reports that have URLs
Running my finished Rails Tutorial app through Rails Best Practices got me a warning
I want to run FindBugs on a project, but we're still stuck with Java
Findbugs reports this: findbugs: [findbugs] Executing findbugs from ant task [findbugs] Running FindBugs... [findbugs]
Running this: bcp MyDb.dbo.uvwMyView out c:\Test.txt -SMyServer -T -c I get this error: SQLState
Running a ruby on rails project on my mac. I need to test it
I am running findbugs and maven with this plugin and then the build fails
Running CodeSniffer PHP style tests gave me the following error: The use of function

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.