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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:04:59+00:00 2026-05-30T04:04:59+00:00

Here’s the situation. Let’s say we have two separate object types, and we want

  • 0

Here’s the situation. Let’s say we have two separate object types, and we want each of them to point to the other. For example:

public class Object1
{
    Object2 obj2;

    public Object1(Object2 obj)
    {
        obj2 = obj;
    }
}

public class Object2
{
    Object1 obj1;

    public Object2()
    {
        obj1 = null;
    }

    public void setObject1(Object1 obj)
    {
        this.obj1 = obj;
    }
}

public class Tester
{
    public static void main(String[] args)
    {
        Object2 obj2 = new Object2();
        Object1 obj1 = new Object1(obj2);
        obj2.setObject1(obj1);
    }
}

Is this allowed in Java? Are there any problems with doing something like this?

  • 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-30T04:04:59+00:00Added an answer on May 30, 2026 at 4:04 am

    Yes, it is allowed in Java. The compiler tracks dependencies in a manner slightly different than C / C++ compilers, so when it detects that you compiled Object1 which needs to use a non-compiled version of Object2, it will also compile Object2.

    As far as the meaning of “contains” in this context, to be technically correct, neither object contains the other, they both contain references to the other. That said, there is no limitation on objects containing references (no matter how circular).

    Of interesting note, the garbage collector also deals with circular references nicely, so if a disconnected circle of referencing objects is created, they will get garbage collected at roughly the same time. Older techniques of garbage collection that work through reference counting could be fooled by circular references, but Java uses a reachability algorithm which determines if the objects are reachable from the main program thread(s).

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

Sidebar

Related Questions

Here is my code...I have two dimensional matrices A,B. I want to develop the
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here is what I want to do. Use this HTML line and have the
Here an example of my checkbox list http://jsfiddle.net/YnM2f/ Let's say I check on G
Here is my code (Say we have a single button on the page that
Here is my situation: I am using telerik with winform. I have a dataset
Here is my situation: I have a controller action called OrderFromCategory which fills a
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior

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.