Domanda

Here is the code:

    timer.schedule(new TimerTask() 
    {
       public void run()
       {
            synchronized(this)
            {
                 try 
                 {   
                   // System.out.println(" ITERATION = ");

                     pachubeCLI.update(78164);  

                 } 
                 catch (PachubeException e)
                 {
                        // If an exception occurs it will print the error message from the
                        // failed HTTP command
                        System.err.println(e.errorMessage);
                 } 
                 catch (IOException e) 
                 {
                        System.err.println(e);
                 } 

             }
        }
    }, 0, 5*1000);

I can tell that the code is basically being used to schedule an operation using an object of the Timer class. The parameters passed to the schedule method , according to eclipse , are (TimerTask task,long delay, long period). But looking at this code , an entire block of code is being passed as the first parameter instead of a reference to the TimerTask class. I've never seen such a method before. What exactly is happening here?

Some background: The schedule method of the Timer object is being used to update a feed on Xively(previously COSM(previously pachube)) periodically.

Also I don't know which tag describes what is happening here.If you do please add it or drop in a comment.

È stato utile?

Soluzione

What you're looking at is known as an "Anonymous Inner Class".

See the javadoc here: http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html

and this question: How are Anonymous (inner) classes used in Java?.

And, for the record, both of the tags you used in your question are appropriate.

Altri suggerimenti

The method schedule here, expects arguments of an object of class type TimerTask, and two other arguments, probably int and time in ms. We are passing an object thorugh the anonymous inner class.

The code is creating an instance of the TimerTask class, but we do not even give the class a name – that's the anonymous inner class.

GuideLines for Anonymous Class.

  1. Anonymous class is declared and initialized simultaneously.
  2. Anonymous class must extend or implement to one and only one class or interface resp.
  3. As anonymous class has no name, it can be used only once.

The logical equivalent of what is happening is:

  • Extending the class TimerTask, e. g. MyTimer extends TimerTask

  • Creating an instance of the new class MyTimer mt = new MyTimer()

  • The actual function call timer.schedule(mt, 0, 5*1000);

The concept is called annonymous inner class. That's what's happening in step one. Step two and three can also be combined. Instances can directly be created where they're needed. That is called annonymous object.

For more insight you should read up on these concepts. They're not hard to grasp. They're often used if you need a standard instance of an interface (for example ActionListener in GUI programming).


For completness' sake, there is also an idiom called double brace initialization. But it's not a good practice in general.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top