GeeCON Prague 2014*
The moose likes Java in General and the fly likes Passing parameters from parent method to an inline class defined inside the method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Passing parameters from parent method to an inline class defined inside the method" Watch "Passing parameters from parent method to an inline class defined inside the method" New topic
Author

Passing parameters from parent method to an inline class defined inside the method

Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 519
    
    2

Hello All,

Though the below snippet works fine, I have couple of questions to better understand on exactly what is happening behind.

Say I have a code like below



As you have noticed from the above code, parameter "param" is being passed to execute method and in turn that is being used inside run() method of inline anonymous implementation of Runnable.

If we keep inline declaration aside, technically this should have been technically equivalent to



and in ExecutorService


So, if we compare the first and second ExecutorService, are the steps to set the param and making it available with in run() method of and others are happening behind the scenes by compiler. If Yes, how would compiler determine, if that should use a setter or a constructor to make sure the param is available to Runnable instance.


Also, is there any prominence in declaring the param as "final" with respect to this kind of method passing.


Please advise.


Regards
KumarRaja

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

your annonymous class declaration looks wrong
should be:


SCJP
Visit my download page
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Hi,

These two methods are not equivalent.
Look at this code:


In this code Runnable 1 and Runnable 2 are objects of the class MyRunnable.
Both of them have each own, independent copy of 'param', and can freely modify it (can modify own copies of param, but not param from the execute method ).

Runnable 3 and Runnable 4 are objects of the anonymous Runnable class,
they both use the same param variable from the enclosing class (method).
Runnables 3 and 4 do not compile because param is declared final (final variables cannot be modified).
Param must be declared final, because anonymous classes can only reference local variables and parameters from the enclosing class or method that are declared final.
In other words, anonymous inner classes cannot modify variables and parameters of the enclosing class/method, can only read them.
If you remove instructions that modify param variable, this code compiles fine.
This is a requirement of Java language semantics - final clause makes that "shared" parameter immutable and therefore thread-safe,
so developer don't have to bother with synchronization in inner classes.

Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 519
    
    2

Randall Twede wrote:your annonymous class declaration looks wrong
should be:


My anonymous class is an implementation of Runnable interface. So it should be declared as new Runnable(){}. Please refer http://docstore.mik.ua/orelly/java-ent/jnut/ch03_12.htm
Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 519
    
    2

Thank you Ireneusz.



 
GeeCON Prague 2014
 
subject: Passing parameters from parent method to an inline class defined inside the method