We can expand your mouse example:
class Mouse {
private String name;
public Mouse() {
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public Mouse makeDropping() {
return new Dropping(this);
}
public static Mouse createMouse(String name) {
Mouse mouse = new Mouse();
mouse.setName(name);
return mouse;
}
}
class Dropping {
private Mouse;
public Dropping(Mouse mouse) {
this.mouse = mouse;
}
public String mouseName() {
return this.mouse.getName();
}
}
We can create a mouse object using the constructor:
Mouse mickey = new Mouse();
mickey.setName("Mickey");
new Mouse() creates the object, mickey is a reference to that object.
We can also create a mouse using the static factory method of the Mouse class:
Mouse jerry = Mouse.createMouse("Jerry");
Mouse.createMouse("Jerry") returns a reference to the Mouse object it created and we set it to jerry.
Now if we call the makeDropping() method on either Mouse, they return a Dropping object.
Dropping mickeyDropping = mickey.makeDropping();
Dropping jerryDropping = jerry.makeDropping();
We don't need to know where the Dropping object is created, we still have a reference to a Dropping object.
String name1 = mickeyDropping.mouseName();
String name2 = jerryDropping.mouseName();
Calling the mouseName() method on the Dropping objects also returns references, this time to a String.