aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes argument anonymous class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "argument anonymous class" Watch "argument anonymous class" New topic
Author

argument anonymous class

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 465
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
I don't understand what you are trying to do.

Place doesn't have a constructor that accepts a Runnable.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
I agree with Keith,
Code is erroneous, and question ambiguous!


Please clarify the question with complete code at least!





cmbhatt


cmbhatt
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 465
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.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: argument anonymous class
 
Similar Threads
overriding
Inner class question
StackOverflowError error at Runtime :(
a question about anonymous question
Is there a way to instantiate anonymous inner class?