• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

argument anonymous class

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand what you are trying to do.

Place doesn't have a constructor that accepts a Runnable.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Keith,
Code is erroneous, and question ambiguous!


Please clarify the question with complete code at least!





cmbhatt
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Keith.your reply made me think a lot.
Now

class Place{
int i=1;
Place(Runnable r){}
}
public class Chapter8 {
public static void main(String[] args) {
Chapter8 t=new Chapter8 ();
Place p=new Place(new Runnable(){
public void run(){
System.out.println(++i);//how could i access Place.i,inside anonymous inner class method
}

});
}
}
anonymous reference p having referencetype Place.Correct?
then is there a way to access Place.i ,inside anonymous class.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

Shiva asked:
h

ow could i access Place.i,inside anonymous inner class method?


Not in the way you tried here.
i is a field in class Place, you are just referring i from the Runnable that does not have an i.
But if you said
System.out.println(++p.i),
it wouldn't work either,because p is method local (local to the main method).
Inner classes can access method local variables only if they are final.
But even if you made Place p final it wouldn't work. Because then you try to access p when it is not yet initialized. And this also won't work in a method local variable.

Perhaps give class Chapter8 an instance field Place p. If you then make your Chapter8 final, it may parially work.


More todo:
All your efforts so far will point to nowhere, because the Runnable provided to the constructor is just lost in the constructor. class Place does nothing with it and it cannot be passed to a thread.
So perhaps class Place needs also a field of type Runnable...


Yours,
Bu.
 
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic