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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:08:27+00:00 2026-05-11T01:08:27+00:00

Recently I worked on FindBugs warnings about exposing internal state, i.e. when a reference

  • 0

Recently I worked on FindBugs warnings about exposing internal state, i.e. when a reference to an array was returned instead of returning a copy of the array. I created some templates to make converting that code easier.

Which one did you create to support defensive programming and want to share with the SO crowd?

Templates I’ve created so far (as examples):

To create a copy of an array to return from a method:

final ${type}[] ${result} = new ${type}[ ${array}.length ]; System.arraycopy( ${array} , 0 , ${result} , 0 , ${array}.length ); 

To clone an object:

(${o}!= null?(${type})${o}.clone():null) 
  • 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-11T01:08:27+00:00Added an answer on May 11, 2026 at 1:08 am

    I like having as a template a "safer" equals() definition:

     /**  * Implement equals based on ${cursor}. <br />  * See {@link #compareTo(Object) compareTo}  * @see java.lang.Object#equals(java.lang.Object)  */ public boolean equals(final Object anObject) {     boolean res = false;     if(anObject == null) { return false; }     if(anObject == this) { return true; }     if(anObject.getClass() == this.getClass())     {         res = this.compareTo(anObject) == 0;     }     return res; } 

    To be sure to always avoid Eq: equals method overrides equals in superclass and may not be symmetric (EQ_OVERRIDING_EQUALS_NOT_SYMMETRIC), where:

    This class defines an equals method that overrides an equals method in a superclass. Both equals methods methods use instanceof in the determination of whether two objects are equal.

    This is fraught with peril, since it is important that the equals method is symmetrical (in other words, a.equals(b) == b.equals(a)).
    If B is a subtype of A, and A‘s equals method checks that the argument is an instanceof A, and B’s equals method checks that the argument is an instanceof B, it is quite likely that the equivalence relation defined by these methods is not symmetric.


    This is only for classes implementing Comparable and allows for:

    • an implementation of equals which is always the same;
    • all comparison logics to be located into one place only (the compareTo() function);
    • the compliance with the javadoc of Comparable#compareTo() asking to ensure that (x.compareTo(y)==0) == (x.equals(y)) (strongly recommended, but not strictly required though).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 94k
  • Answers 94k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You probably should override the delete operator. Example for the… May 11, 2026 at 6:47 pm
  • Editorial Team
    Editorial Team added an answer I believe it was in "Working Effectively with Legacy Code"… May 11, 2026 at 6:47 pm
  • Editorial Team
    Editorial Team added an answer You need to set the environment variable with mod_rewrite too:… May 11, 2026 at 6:47 pm

Related Questions

Recently I've been revisiting an old project, which I last worked on about two
From what I have read best practice is to have classes based on an
Let me preface by saying I am not knocking .Net (it helps me earn
Commonly when I look around the Internet, I find that people are generally using

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.