java - How to make two classes reference the same object and its methods -
so i'm trying make object i've created have same variables , methods corresponding object in second class without using static methods etc. best way can think of doing perhaps using method goes like:
public void setobjectreference(object object){ object = this.object; } my problem want reference few objects between classes , approach mean i'd have make method different this.object; changing name of each method. seems inefficient way go , i'm sure there's better way make 2 objects in 2 classes reference same instance of said object.
cheers
edit:
i trying use , object called name, in object there few methods, namely getname() setname() etc. want able set value using setname() in first class , use in second class using getname(). trying implement in javafx application communicating between controllers
hope helps somewhat
you on right way mixing objects , names. don't need same name access same value - need same object. 1 of approaches co communicate between 2 classes have common object , use in both classes. this
class context{ string name; public void setname(string name){ this.name=name; } public string getname(){ return name; } } class controller1{ context context; public controller1(context context){ this.context=context; } public void somemethod(){ context.setname("alex"); } } class controller2{ context context; public controller2(context context){ this.context=context; } public void anothermethod(){ string namefromfirtscontroller=context.getname(); } } of course lot missing including synchronization , there more efficient approaches observer/observable or messaging talking approach , want show basic idea.
p.s. mean
this.object = object; to save value within class instance.
Comments
Post a Comment