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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:37:22+00:00 2026-05-20T23:37:22+00:00

In my program I have two classes, one called GlassPiece, and one called TrackerChip.

  • 0

In my program I have two classes, one called GlassPiece, and one called TrackerChip.

These two objects are always “strongly connected”, that is, no two GlassPieces can share a TrackerChip, and no two TrackerChips can share a GlassPiece. Therefore in my setter methods, I need to take care to disconnect any old references hanging around, as so:

public class TrackerChip
{
    GlassPiece linkedGlassPiece;

    public void setGlassPiece(GlassPiece newGlassPiece)
    {
        GlassPiece oldGlassPiece = linkedGlassPiece;

        linkedGlassPiece = newGlassPiece;

        if(oldGlassPiece != null)
        {
            oldGlassPiece.setTrackerChip(null); //disconnect old GlassPiece
        }

        if(linkedGlassPiece != null && linkedGlassPiece.getTrackerChip() != this)
        {
            linkedGlassPiece.setTrackerChip(this); //update counterpart
        }
    }
}

and the method GlassPiece.setTrackerChip(TrackerChip) works exaxctly the same way.

The thing is, the above code doesn’t actually work, and strange stuff happens when trying to manage linking between several different GlassPieces and TrackerChips. However, if I replace the last part with:

    if(newGlassPiece != null && newGlassPiece.getTrackerChip() != this)
    {
        newGlassPiece.setTrackerChip(this);
    }

Then everything works properly. This seems very strange to me (all I did was replaced linkedGlassPiece, the instance variable, with newGlassPiece, the parameter). But early in the method I set the references equal to each other! Why does the first method not work?

P.S. I can confirm there is no infinite loop in the method.

  • 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-20T23:37:23+00:00Added an answer on May 20, 2026 at 11:37 pm

    As for why this isn’t working, you’re right, it won’t hit an endless loop, but it’s not going to do what you expect.

    1. You enter setGlassPiece, linkedGlassPiece for this object is set to the value of newGlassPiece.
    2. Then it calls setTrackerChip(null) on the oldGlassPiece.
    3. The oldGlassPiece still has a reference to the original TrackerChip, so it calls setGlassPiece(null), which sets linkedGlassPiece to null that you just set on the TrackerChip, and calls setTrackerChip(null) on the NEW GlassPiece as well.

    I honestly can’t think of a way to get it to work the way you’re going. You would have to add some additional parameters such that it would no longer be re-entrant. Namely, when you call setTrackerChip on the oldGlassPiece, it’s not going to turn around and call the same TrackerChip back setting its reference to null. Perhaps just a boolean flag that would indicate that it should not null out the second level references.

    Here’s some code:

    public class TrackerChip
    {
        GlassPiece linkedGlassPiece;
    
        public void setGlassPiece(GlassPiece newGlassPiece)
        {
             setGlassPiece(newGlassPiece, true);
        }
    
        public void setGlassPiece(GlassPiece newGlassPiece, boolean reentrant)
        {
            GlassPiece oldGlassPiece = linkedGlassPiece;
    
            linkedGlassPiece = newGlassPiece;
    
            if(reentrant && oldGlassPiece != null)
            {
                oldGlassPiece.setTrackerChip(null, false); //disconnect old GlassPiece
            }
    
            if(linkedGlassPiece != null && linkedGlassPiece.getTrackerChip() != this)
            {
                linkedGlassPiece.setTrackerChip(this); //update counterpart
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two scripts that are called in one program, the first script creates
Hi I have a program where there are two domain classes. One that has
I have a program that compares two classes in a series of tests. The
I have two classes that are almost identical, besides one method. The classes have
I have two classes. one to run the program and the other for being
In my program I have two classes: Collector and Entity. Collector stores Entities in
I have a strange Hibernate behaviour in my program. I have two classes with
I have my program that can draw rectangles. I have two problems I can't
I have two programs: one CLI program, and one GUI. The GUI is a
I have two classes; one is a server, another one is a client, and

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.