0

I have the below timed task:

static TimerTask timedTask = new TimerTask() {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println("timed task");
    }
};

//main method
main(...) {
Timer timer = new Timer();
    timer.schedule(timedTask, (long) logfile.getFileHash().get(1).getTimeStampInMilli());
}

what I want to do is, to create a class that exteds TimerTask so that I can create a new timerTask when ever i want. but the problem is when i create the class as follows:

class TimerTask2 extends TimerTask {

    @Override
    public void run() {
        // TODO Auto-generated method stub

    }
}

the line

timer.schedule(new TimerTask2(), (long) logfile.getFileHash().get(i).getTimeStampInMilli()); is higlighted by ecipse and says:

No enclosing instance of type File_IO is accessible. Must qualify the allocation with an enclosing instance of type File_IO (e.g. x.new A() where x is an instance of File_IO).

i tried to qualify te class instance with the main class name but also it did not work.

kindly please provide suggestions for that.

Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • Possible duplicate of [Java - No enclosing instance of type Foo is accessible](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian Mar 04 '16 at 00:01

1 Answers1

0

add static modifier to TimerTask2:

static class TimerTask2 extends TimerTask {
    @Override
    public void run() {
    }
}
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • this is because main() is static and TimerTask2 is defined inside main's class. This is the same as if you tried to read a non-static field from static method. – Evgeniy Dorofeev Dec 03 '14 at 13:12