Java send data to class where object was initialized -
we have simple initialization: example:
class1 { public class1 { class2 object = new class2(); } public somemethod(string anystring) {...} }
so best way call somemethod in class1 class2 , pass "anystring" value? i'm using custom events , interface. maybe there better, more rational way this?
but maybe there better, more rational way this?
yep.
class1 { public class1() { class2 object = new class2(this); } public somemethod(string anystring) {...} } class2 { public class2(class1 parent) { parent.somemethod("a string value"); } }
explanation
simply pass instance of parent object child class. child class can call desired method directly.
java naming conventions
the convention states methods start lower case letter.
somemethod -> somemethod
Comments
Post a Comment