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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:44:04+00:00 2026-06-07T02:44:04+00:00

There is a question on Stack Overflow on why starting a thread inside the

  • 0

There is a question on Stack Overflow on why starting a thread inside the constructor is not a good idea . I realised that the result of such a thing will be that ‘this’ can escaped.I also read that publishing a EventListener from constructor is also a bad idea for the same reason . What are the other patterns which I should be aware of in which ‘this’ can escape ?

  • 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-07T02:44:06+00:00Added an answer on June 7, 2026 at 2:44 am

    Calling any instance method of your object from the constructor leaks this to that mathod. This may be OK as long as that method is under your control (not publicly overridable), and you are making sure you don’t leak this further out from it. Using this as an argument to any method is, of course the more explicit variant, and that happens when you say x.addEventListener(this). A perhaps more insdidious, since less obvious, way to leak a this is to not use this itself as an argument, but an instance of an inner/local/anonymous class, say

    public class Main 
    {
      private class MyListener extends MouseAdapter { ...}
    
      public Main() {
        class Listener1 extends MouseAdapter { ... }
        someSwingComponent.addMouseListener(new MyListener()); // inner class
        someSwingComponent.addMouseListener(new Listener1()); // local class
        someSwingComponent.addFocusListener(new FocusAdapter() { ... }); // anonymous
      }
    }
    

    In all these cases this will be the enclosing instance of the object passed as a method argument. If, on the other hand, you declare a static nested class, it will not have an enclosing instance.

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

Sidebar

Related Questions

A basic question but there is no such question on Stack Overflow (for ASP.NET)
Hello there Stack Overflow, I hope you'll help me with my very first question
Hey there guys, this is my first question on Stack Overflow. I figured this
I'm not sure if this an appropriate question for stack overflow. If not, I
I know there's another question like this on stack overflow, but my problem is
this is my first question on Stack Overflow. This is not really a programming
I know there are a thousand data import related questions on Stack Overflow, and
There are a lot of questions relating to license keys asked on Stack Overflow.
There is a question here in stackoverflow with the same title but that is
My colleague wrote the following stackoverflow question: other stack overflow question on this topic

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.