Lets say there is tow class 1) User 2) Inventory
I want to initialize User Object with help of inventory Object and Inventory Object with help of User Object
See How???
Step1 )
package com.myapp.circulardependency;
public class User {
private final Inventory inventory;
/*
* User(Inventory inv) { inventory = inv; }
*/
// other object created with this obejct
User() {
inventory = new Inventory(this);
}
public void show() {
System.out.println("User");
}
}
I want to initialize User Object with help of inventory Object and Inventory Object with help of User Object
See How???
Step1 )
package com.myapp.circulardependency;
public class User {
private final Inventory inventory;
/*
* User(Inventory inv) { inventory = inv; }
*/
// other object created with this obejct
User() {
inventory = new Inventory(this);
}
public void show() {
System.out.println("User");
}
}
Stpe 2)
package com.myapp.circulardependency;
public class Inventory {
private final User owner;
Inventory(User own) {
owner = own;
}
public void show() {
System.out.println("Inventory");
}
}
3) Now text your findings.
package com.myapp.circulardependency;
public class CircularDependencyDemo {
public static void main(String[] args) {
User u = new User();
u.show();
Inventory i = new Inventory(u);
i.show();
}
}
Cheers !!!