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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:26:41+00:00 2026-05-27T11:26:41+00:00

Suppose I create an instance of this Test class, which starts a new Thread

  • 0

Suppose I create an instance of this Test class, which starts a new Thread using a nested class :

public class Test
{
    private String _str;

    public Test()
    {
        _str = "Hello world !";
        new TestThread().start(); // Start a background thread
    }


    private class TestThread extends Thread
    {
        public void run()
        {
            while (true)
            {
                System.out.println(_str);
                Thread.sleep(1000);
            }
        }
    }
}

What happens now if I remove any strong references to my Test instance ? The background thread will still be alive and will continue to print the _str variable, but I’m afraid that the GC can collect it anytime.

Of course I can create a local copy of the reference (with a constructor : public TestThread(String str) and store it in a local variable) but I wonder if this is necessary.

  • 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-05-27T11:26:41+00:00Added an answer on May 27, 2026 at 11:26 am

    TestThread itself has a strong reference to Test, so as long as it is not collectable, the instance of Test won’t be collectable. Since it’s a running thread, the instance of TestThread obviously isn’t available for collection.

    When you create a non-static inner class (TestThread), every instance of that class has an implicit reference to an instance of its enclosing class (Test).

    See the Inner Classes section of the Nested Classes Java Tutorial.

    You don’t have to be “afraid” of what the GC might do. It won’t collect an object that you could possibly reference, unless you’re going out of your way to work with weak references.

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

Sidebar

Related Questions

I am doing something like this: class Class(object): def __init__(self): self.var=#new instance name string#
Suppose you create a class names Person using the builder pattern, and suppose the
Let's suppose you have this class: class A { public: A () {} A
Assumptions Suppose I have a class with a property: class ClassWithProperty { public string
Suppose that I have a class like this class Employee: pass I create two
suppose we have this class structure: public class BaseClass { public BaseClass() { Console.WriteLine(Base
Suppose you create a generic Object variable and assign it to a specific instance.
This is about inheritance in JavaScript. Suppose I create a constructor Bird(), and another
Suppose I have the following: using(var ctx = DataContextFactory.Create(0)) { ... Some code ...
Suppose I am designing a class that can handle any database technology to create

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.