Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different object references for the same(?) object

I have asked about getting unique instance of class from another class recently.

( How to get specific instance of class from another class in Java? )

So, I am trying to make it work:

My Application:

public class Application
{

    // I will always have only one instance of Application

    private static Application _application;

    // Every instance of Application (one in my case) should have its own View

    private View view;

    // Constructor for new instance of Application

    private Application()
    {
        view = new View();
    }

    // Getter for my unique instance of Application

    public static Application getSharedApplication()
    {
        if (_application == null)
            _application = new Application();
        return _application;
    }

    // Main class

    public static void main(String[] args)
    {
        // So I'm accessing my instance of Application
        Application application1 = getSharedApplication();

        // Here is object reference
        System.out.println(application1);

        // And now I'm accessing the same instance of Application through instance of View
        Application application2 = application1.view.getApplication();

        // Here is object reference
        System.out.println(application2);
    }

}

My View:

public class View
{

    // I'm accessing my instance of Application again

    public static Application application = Application.getSharedApplication();

    // This method should return my unique instance of Application

    public Application getApplication()
    {
        return application;
    }

}

The problem is that main method returns different object references.

Application@1430b5c
Application@c2a132

What's wrong with my code?

like image 915
Edward Ruchevits Avatar asked Aug 16 '12 17:08

Edward Ruchevits


People also ask

Can multiple object variables contain references to the same object?

Yes, two or more references, say from parameters and/or local variables and/or instance variables and/or static variables can all reference the same object.

Which checks if 2 references point to the same object?

Comparing for reference equalityThe == operator can be used to check if two object references point to the same object.

How many objects can be referenced from the same variables?

How many objects can be referenced from the same variables? Explanation: There should not be any confusion in how many references can be made from a single variable. A single variable can only point to one object at a time.

Can two objects reference each other?

Of course you can have objects reference each other. You could simply pass the this pointer in both objects to each other, which is perfectly valid.


2 Answers

Here is what happens:

  • the program first calls Application application1 = getSharedApplication();
  • that in turn calls the static method which calls new Application() - that call needs to load the View class, which is a member of Application.
  • the View class is loaded and its static member is initialised and runs getSharedApplication(); (note that at this stage, _application is still null). That creates a new Application() too

You now have 2 Application instances.

Note that if you add View v = new View(); as the first line of your main, you only have one instance of Application (which gets loaded once from the static variable of View). That makes sense when you think hard about it but is not very intuitive...

like image 92
assylias Avatar answered Oct 04 '22 07:10

assylias


The general answer to such questions is: Use a debugger! For instance, you might set a break point in the constructor of Application, run your program, and inspect the stack when the constructor executes for the second time.

If you do this, you'll note something like this:

Application.<init>() line: 21   
Application.getSharedApplication() line: 31 
View.<clinit>() line: 59    
Application.<init>() line: 23   
Application.getSharedApplication() line: 31 

That is, the view wants to get the shared application before the shared application has been fully constructed (and before the latter is stored in the static field).

like image 38
meriton Avatar answered Oct 04 '22 05:10

meriton