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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:33:04+00:00 2026-06-14T05:33:04+00:00

How many objects are eligible for garbage collection when the main method of the

  • 0

How many objects are eligible for garbage collection when the main method of the Tester class reaches its end? My impression is that the answer is two, particularly a1, b1. However I have found somewhere as corect answer, that only a1 object is eligible. I think that since we have not asigned b1 as member variable in a2 an b1 is assigned to null before main ends, it should be collected by garbage collector. What is true?

class B {
}

class A {
    static B b1;
    B b2;
}

public class Tester {
    public static void main(String[] args) {
        B b1 = new B();
        B b2 = new B();
        A a1 = new A();
        A a2 = new A();
        a1.b1 = b1;
        a1.b2 = b1;
        a2.b2 = b2;
        a1 = null;
        b1 = null;
        b2 = 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. Editorial Team
    Editorial Team
    2026-06-14T05:33:05+00:00Added an answer on June 14, 2026 at 5:33 am

    The object initially assigned to the method scope variable b1 is not eligible for collection because the reference to it in Class A is static. It does not expire with that particular instance of a1. It is a sneaky bit of wonky java syntax that a1.b1 and A.b1 are the same reference, but it is what it is. That reference remains live until the class A is un-loaded/the JVM exists, regardless of what happens to any instance of A such as a1.

    The code assigns the pointer b1 in the method to null, but it does not assign A.b1 to null.

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

Sidebar

Related Questions

I have many objects reference the same class of stored data. In previous programs,
How can I find out how many objects are created of a class in
I have a LinkedList that contains many objects. How can I find the number
How would I show one of many nested objects in the index view class
This is a quesiton taken from a java exam, How many objects are eligible
I have @table_name(object array) contain many objects. I want to replace its column values
i have many objects of the same custom class, and another many objects of
I have a list that contains many objects.I want found an object in this
I have many objects in one page which xpath is: //td[@class='bxec-day' or @class='bxec-day bxec-holiday'
So I have many objects with materials that each possess different properties (brick, glass,

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.