0

I have some code like this:

package somepackage.somefolder;
public class MyClass {
    public MyClass () {
        new Thread() {
            public void run() {
                SomeOtherClass something = new SomeOtherClass(REFERENCE-TO-MyClass-NEEDED!!!);
            }
        }
    }

    public someFunction(){
        //do stuff...
    }
}

package somepackage.someOtherfolder;
public class SomeOtherClass {
    public SomeOtherClass (MyClass mc) {
        mc.someFunction();
    }
}

How to send reference of the actual instance of MyClass to the other class so it could access MyClass someFunction()?

If I call it directly like:

SomeOtherClass something = new SomeOtherClass (this);

It points to Thread itself where I need it to point to the parent class it was started from, that is MyClass.

I cannot reference it as MyClass mc = new MyClass inside SomeOtherClass as it would create new instance of the class.

qraqatit
  • 492
  • 4
  • 14
  • 1
    `new SomeOtherClass(MyClass.this);` – Hovercraft Full Of Eels Sep 07 '19 at 13:22
  • 1
    @HovercraftFullOfEels Yes, thank you very much - that is the correct answer (I was so close)...but I do not know how to flag your answer as the right one? – qraqatit Sep 07 '19 at 18:50
  • You don't. The question is a duplicate of many previous similar questions, and I have closed it as a duplicate (please see the link to the question and answer at the top of your question). I also added a comment to summarize the salient point in the answer to give you a quick and dirty answer as well. – Hovercraft Full Of Eels Sep 07 '19 at 20:36

0 Answers0