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?
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.
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? 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.
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.
Here is what happens:
Application application1 = getSharedApplication();
new Application()
- that call needs to load the View class, which is a member of Application
.getSharedApplication();
(note that at this stage, _application
is still null). That creates a new Application()
tooYou 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...
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With