This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

final Runnable  RSS feed

 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


runnable might not have been intialiized at doSomething(runnable).



 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things:
1. What is your question?
2. What is the point of this code?
 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


How to initialize the value for final Runnable ?

If we initialize runnable to null, gives can not assign a value to runnable. Bec' it is final.

 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's wrong with:
 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator





variable runnable might not have initialized.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In what circumstance would you want to do this?
 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you have to use the exact same runnable in doSomething()?
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you can have a circular dependency and ensure it doesn't compile?
 
Dave Tolls
Rancher
Posts: 2911
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely you're just passing 'this' in?

For what purpose I don't know, as Runnable doesn't exactly hold a great deal of information...and isn't intended to.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Surely you're just passing 'this' in?


Agreed. It doesn't make sense. The OP is saving the instance of the anonymous inner class, in a local reference variable, so that the instance of the anonymous inner class can access it... in other words, the anonymous inner class is trying to access a reference to itself. This is what the "this" variable is used for.



As for more details of the circular dependency issue... the instance of the inner class is trying to access a local variable of the outer class method. For this to work, the local variable has to be effectively final *and* assigned before the inner class is instantiated. This means that you need to instantiate the inner class *and* assign it to the reference variable -- and do so before you instantiate the inner class. This is, of course, not possible.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!