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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:15:59+00:00 2026-06-08T19:15:59+00:00

class Beta { } class Alpha { static Beta b1 ; Beta b2; }

  • 0
class Beta { }

class Alpha {
static Beta b1  ;
Beta b2;
}

public class Tester {

public static void main(String[] args) {

    Beta b1 = new Beta();
    Beta b2 = new Beta();
    Alpha a1 = new Alpha();
    Alpha a2 = new Alpha();

    a1.b1 = b1 ;
    a1.b2 = b1 ;
    a2.b2 = b2 ;

    a1 = null ;
    b1 = null;
    b2 = null;

    System.out.println(" Line 1    " + " a1   "  + a1.b1);
    System.out.println(" Line 2    " + " a1   "  + a1.b2);
    System.out.println(" Line 3    " + " a2   " + a2.b2);
    System.out.println(" Line 4    " + " a1   " + a2.b1);
    System.out.println(" Line 5    " + " b1   " + b1);
    System.out.println(" Line 6    " + " b1   " + b2);

  }

 }  

I am not sure why only a1 object is available for garbage collection in the above program. I am expecting a1 , b1 and b2 to be collected by garbage collector.

As you can see a1 , b1 and b2 were made NULL hence this is making objects to be available for garbage collection. If object is NULL or unreachable by any thread or reference variable it should be collected by garbage collector.

Can someone help me understand the subtleness of the above program with good example and in more precise way ? Your help is appreciated.

  • 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-08T19:16:01+00:00Added an answer on June 8, 2026 at 7:16 pm

    Because there is still reference exists to objects pointed by b1 & b2 refrences due to below lines.

    a1.b1 = b1 ;
    a1.b2 = b1 ;
    a2.b2 = b2 ;
    

    Assume like this:

      b1--->BetaObj1
        b2---BetaObj2
        a1---> AlphaObj1
        a2---> AlphaObj2
    

    a1.b1 points to b1 that means, there is reference to BetaObj1
    a1.b2 points to b1 that means, there is another reference to BetaObj1

    (At this point there are 3 references to BetaObj1)

    a2.b2 points to b2 that means, there is reference to BetaOBj2

    (At this point there are 2 references to BetaObj2)

    a1=null; makes AlphaObj1 eligible for GC

    b1=null; makes BetaObj1 reference count to 2, so this object is not eligible for GC

    b2=null; makes BetaObj2 reference count to 1, so this object is not eligible for GC.

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

Sidebar

Related Questions

class Test { public static void main(String[] args) throws Exception { Test t =
import javax.swing.*; import javax.swing.event.*; public class NewGUIStuff{ public static void main(String args[]){ NewGUIStuff gui
class a{ public void foo(int a){ System.out.println(super); } } class b extends a{ public
The following code template<typename T, typename U> class Alpha { public: template<typename V> void
I have two domain classes, Alpha and Beta. class Beta{ String betaName int age
Class 1 private void checkDuplicateCustomer(BulkCustomerVO bulkCustomerVO) { PagedDuplicateCustomerVO duplicateCustomerVO = new PagedDuplicateCustomerVO(); duplicateCustomerVO.setCustomer(bulkCustomerVO.getCustomerVO()); duplicateCustomerVO
class X { int i; public: X() { i = 0; } void set(int
I have 3 classes: public class Alpha { public Number number; } public class
template<class T,class TV> struct CONJUGATE_GRADIENTS_HELPER{ DEFORMABLE_OBJECT<T,TV>* deformable_object; int partition_id; T time,dt; T alpha,beta; ARRAY<TV>*
How to improve my attempt: class gotClass { protected $alpha; protected $beta; protected $gamma;

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.