John
John

Reputation: 3460

How to get instance of my object inside a Timer

I'm learning Java right now, I have timer inside one of my functions like so:

public class SomeClass {
    private Timer timer = new Timer();

    private void someFunction() {
        timer.schedule(new TimerTask() {
            public void run() {
               // here lies the problem
            }
        },
        1000);
    }
}

What I want to do is call another function someOtherFunction(SomeClass c) that takes as an argument an instance of SomeClass. Outside of the timer, I could simply say someOtherFunction(this), but inside the timer that doesn't work since this is a TimerTask.

What can I do to get the instance of SomeClass inside my timer?

Upvotes: 2

Views: 387

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Just use SomeClass.this to get the outer class instance inside of the inner class.

Upvotes: 5

Related Questions