0

I have a class as follows:

private class LanePair {

    public int cameraNumber;
    
    public Nest nest1, nest2;
    public LanePairStatus status = LanePairStatus.TIMER_OFF;
    Timer timer = new Timer();

    public LanePair(int cameraNunber, Nest nest1, Nest nest2) {
        this.cameraNumber = cameraNumber;
        this.nest1 = nest1;
        this.nest2 = nest2;
    }

    public void startTimer() {
        status = LanePairStatus.TIMER_ON;
        timer.schedule(new TimerTask() {
            public void run() {
                DoAskForLaneClear(/*I want to pass this class (LanePair) here*/);
            }
        }, 6000 ); // 6 seconds
    }
    
    public void stopTimer() {
        timer.cancel();
    }

}

The thing is, I can't figure out how to do it. Any suggestions?

Related:

Community
  • 1
  • 1
Justin
  • 305
  • 2
  • 12
  • 1
    That's not a subclass but an inner class. The title of the question is misleading. I change it but kept the original, because otherwise the question won't make much sense as you would've know partially the answer. – OscarRyz Dec 04 '09 at 21:42

5 Answers5

10
DoAskForLaneClear(LanePair.this);

Since you can't use this (it will reference the TimerTask), but your anonymous class can't exist wihtout an instance of LanePair, that instance is referenced by LanePair.this

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • This seems to have done the trick. Can you explain how/why it works so I can make sure I'm doing the right thing? It passes the instance of the LanePair that the run method is "in", right? Thanks! – Justin Dec 04 '09 at 20:59
  • yes. since you can't use 'this', but your anonymous class can't exist wihtout an instance of LanePair, that instance is accessed by LanePair.this – Bozho Dec 04 '09 at 21:02
  • I've used that more often than I would like to after I started using Wicket. It does the trick, thought. – Ravi Wallau Dec 04 '09 at 22:46
0

I think you want either

DoAskForLaneClear(this);

or

this.DoAskForLaneClear();

I don't see where DoAskForLaneClear() is defined though.

Scott Saunders
  • 29,840
  • 14
  • 57
  • 64
  • Oh, sorry, DoAskForLaneClear(LanePair lanePair) is a method that is defined elsewhere. I'm trying to pass in the LanePair that the timer is inside. – Justin Dec 04 '09 at 21:02
0

I do this sometimes:

        public void startTimer() {
            status = LanePairStatus.TIMER_ON;
            **final LanePair lanePairRef = this;**
            timer.schedule(new TimerTask() {
                    public void run() {
                            DoAskForLaneClear(**lanePairRef**);
                    }
            }, 6000 ); // 6 seconds
    }
Zak
  • 24,947
  • 11
  • 38
  • 68
0

If you want to use in any case this word for example :)

import java.util.Timer;
import java.util.TimerTask;

public class Test
{      


    public void doAskForLaneClear(LanePair lanePair){
        //Action of lanePair
    }

    private class LanePair {      
        public int cameraNumber;        
        public Nest nest1, nest2;        
        public LanePairStatus status = LanePairStatus.TIMER_OFF;
        Timer timer = new Timer();    


            public LanePair(int cameraNumber, Nest nest1, Nest nest2) {
                this.cameraNumber = cameraNumber;            
                this.nest1 = nest1;         
                this.nest2 = nest2;
                startTimer();
            }      

            public void startTimer() {     
                status = LanePairStatus.TIMER_ON;           
                timer.schedule(new MyTimerTask(this), 6000 ); // 6 seconds       
            } 

            public void stopTimer() { 
                    timer.cancel();   
           }

    }

    private class MyTimerTask extends TimerTask{

        private LanePair lanePair;

        public MyTimerTask(LanePair lanePair){
            this.lanePair = lanePair;
        }

        @Override
        public void run() {
            Test.this.doAskForLaneClear(lanePair);
        }

    }

}
bora.oren
  • 3,439
  • 3
  • 33
  • 31
-1
LanePair.class

?

Jonathan Feinberg
  • 44,698
  • 7
  • 80
  • 103