Reputation: 63
I have two classes and one other object. I need to get the same object (the same instance) in these classes and ability to modify it form these classes. In first class I only need getter of this object, in second class I need setter.Both classes are in one more class(Let's call her the third one) on the basis of composition. I thinking about passing reference from third class to first and second, but during it, it will be null. If I set it to some value from class second, the reference in class first, still will be reference to null? Is it any better and proper way to do this?
class First {
SomeClass object;
public void setObject(SomeClass object) {
this.object = object;
}
public SomeClass getObject() {
return object;
}
}
class Second {
SomeClass object;
public void setObject(SomeClass object) {
this.object = object;
}
public SomeClass getObject() {
return object;
}
}
class Third {
First first;
Second second;
SomeClass object;
public void initializeClasses(){
first.setObject(object);
second.setObject(object);
}
}
But in the momment of initializeClassed the reference is null. So if I after that set in Second class it will be the same object in First class?
Upvotes: 0
Views: 953
Reputation: 403
In your code -- Class First and class Second both have reference variable for SomeClass and both are null and are not connected. -- if you set SomeClass object to the new value in second class First class value will still refer to null.
Solution -- pass an initialized object so that SomeClass reference in class First and class Second points to the same object in memory (not null) -- never set the SomeClass reference to the new object in Second Class this will make SomeClass reference to point to separate objects in class First and Class Second -- instead, change the properties of the SomeClass object referred by SomeClass reference in Class Second to see the changes getting reflected in First Class
hope it helps :)
Upvotes: 0
Reputation: 400
You can do simply like static object in Third class. Hope this will help you.
class First
{
SomeClass object;
public void setObject(SomeClass object)
{
this.object = object;
}
public SomeClass getObject()
{
return Third.second.getObject();
}
}
class Second
{
SomeClass object;
public void setObject(SomeClass object)
{
this.object = object;
}
public SomeClass getObject()
{
return object;
}
}
public class Third
{
static First first;
static Second second;
SomeClass object;
public void initializeClasses()
{
first = new First();
second = new Second();
object = new SomeClass();
System.out.println(object);
second.setObject(object);
System.out.println(first.getObject());
}
public static void main(String [] args)
{
Third t = new Third();
t.initializeClasses();
}
}
Upvotes: 0
Reputation: 14999
For Java 8 or later, you can postpone the actual access to SomeClass
by using a Supplier<SomeClass>
rather than a reference. You'd do it like this:
class A {
private Supplier<SomeClass> objectSupplier;
public SomeClass getObject() {
return objectSupplier.get();
}
public void setObjectSupplier(Supplier<SomeClass> supplier) {
objectSupplier = supplier;
}
}
and create it like this:
class C {
class A a;
private SomeClass object;
public void initClasses() {
a.setObjectSupplier(() -> object);
}
}
This way, C
's instance a
will always return C
's object
, even if it changes in C
in the meantime.
Upvotes: 1