java - What class has access to my methods? -
my question today creating object, , other classes have access methods. trying larn threading, since jpanels don't back upwards threads have gotten confused.
i create simplified class this:
public class mymethodclass { public mymethodclass () { myclass myclass = new myclass(); } public void mymethod() { //do variables } }
so have new class object called myclass (myclass class, content not important). public, myclass
have access mymethod
?
if not, there way pass re-create mymethodclass
myclass
can utilize mymethod
, knowing mymethodclass
created myclass
in first place?
if class myclass
nested class, access mymethod
?
myclass
not able access methods within mymethodclass
unless creates instance of , in case, that's bad idea.
you could pass myclass
instance of mymethodclass
via constructor or setter method, you'd improve using mutual interface, decouple 2 classes , improve reusability
start defining contract between 2 class (as interface)...
public interface somemethods { public void mymethod(); }
add ability pass implementation of somemethods
myclass
via it's constructor or setter method...
public class myclass { private somemethods somemethods; public myclass(somemethods somemethods) { this.somemethods = somemethods; } public void somework() { somemethods.mymethod(); } }
have mymethodclass
implement somemethods
interface , pass reference of myclass
public mymethodsclass implements somemethods { public mymethodclass () { myclass myclass = new myclass(this); } @override public void mymethod() { //do variables } }
just beware, it's not considered thought pass this
other classes or methods within constructor, state of object may not realised , value methods/classes rely on may not yet initialised.
java object methods nested
No comments:
Post a Comment