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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:27:28+00:00 2026-06-17T09:27:28+00:00

I am using the NetBeans IDE and it is giving me a warning that

  • 0

I am using the NetBeans IDE and it is giving me a warning that does not make sense to me. The warning states “Leaking this in constructor”. The following code is the basic setup (I just removed code irrelevant to the issue). Basically I just want to keep a list of all Square objects made. Is this a warning I need to worry about? Or is it just the possible cause of a memory leak depending on the situation?

Either way, can someone explain why this would be considered a leak?

public class Square {
    private static ArrayList<Square> squares;

    public Square() {
        if(squares == null) {
            squares = new ArrayList<>();
        }

        squares.add(this); // I get a warning on this line
    }
}

I know it is just a warning, but I don’t like to ignore warnings unless I fully understand what is going on and can make the informed choice for a specific situation.

Thanks!

  • 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-17T09:27:28+00:00Added an answer on June 17, 2026 at 9:27 am

    I don’t think the warning concerns garbage collection, although that is indeed a problem here. The instances will never be GC’ed (unless the whole ClassLoader is collected).

    The warning is saying that this is being passed to another method from within the constructor. Before the constructor finishes, this is not necessarily a fully-formed and initialized object according to the logic enshrined in the constructor. Anything in the constructor is intended to happen before anything else gets its hands on the object. But something else is getting to use this before the constructor finishes. That could cause surprising bugs.

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

Sidebar

Related Questions

I'm using NetBeans IDE 6.9 (Build 201006101454), and wrote this simple Java class (that
I am using NetBeans IDE but I am using Glassfish externally(means not that which
I am using Netbeans IDE for a java project. In this project i need
I am a newbie java dev using netbeans IDE 7.1.1 and im watching this
I'm using Netbeans IDE to make a gui application. I have a JFrame with
I'm using NetBeans IDE 7.2.1 with git. Somehow NetBeans is not displaying the change
I'm using NetBeans IDE 6.9.1 for developing java/javafx applets, this netbeans version generates only
I'm using Netbeans IDE as for PHP developemnt. At this moment I'm looking on
I'm using NetBeans IDE 7.1 From this IDE I run first build and wait
I am using Netbeans IDE. I want to give the prompt text to JTextfield

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.