Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is happening in this Java code snippet? [duplicate]

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.

like image 999
Flame of udun Avatar asked Sep 09 '13 18:09

Flame of udun


People also ask

What is duplicate code in Java?

Simply put, it's when a snippet of code appears multiple times throughout a codebase. It happens for many reasons: Somebody wanted to reuse a function in a different class, and copy-paste was the quickest solution.

What does duplicated code fragment mean?

Code Inspection: Duplicated code fragmentReports duplicated blocks of code from the selected scope: the same file or the entire project. The inspection features quick-fixes that help you to set the size of detected duplicates, navigate to repetitive code fragments, and compare them in a tool window.

What is duplicated in SonarQube?

SonarQube is telling you that this portion of the code contains duplicated logic. This doesn't necessarily mean that the code itself is copy-pasted, but that, conceptually, the exact same thing is happening at multiple places.

What is duplicate in programming?

In computer programming, duplicate code is a sequence of source code that occurs more than once, either within a program or across different programs owned or maintained by the same entity. Duplicate code is generally considered undesirable for a number of reasons.


2 Answers

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.

like image 72
StormeHawke Avatar answered Sep 28 '22 03:09

StormeHawke


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.
like image 37
JNL Avatar answered Sep 28 '22 01:09

JNL