This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous Inner Class Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anonymous Inner Class Problem" Watch "Anonymous Inner Class Problem" New topic
Author

Anonymous Inner Class Problem

Philani Dlamini
Greenhorn

Joined: Apr 07, 2005
Posts: 23
Hi Everone.
In my practice code snippet im passing a local anonymous inner class to a method that expects an interface type. Im also using a plain old anonymous inner class to sub-class and add some functionality to my ferrari class.

Can someone please explain to me why my output returns only:

"Ferrari travels at 390 KPH"

and does not output:

"The ferrari has stopped"

-----------------------------------------------


public class Parent{

public static void main(String argsp[]){

Ferrari f = new Ferrari();
f.Foward(new Car(){
public void go(){
System.out.println("Ferrari travels at 390 KPH");
}
}
);
f.Stop();

}
}

class Ferrari{

public void Foward(Car object){

object.go();
}

public void Stop(){
Ferrari fi = new Ferrari(){
public void Stop(){
System.out.println("The ferrari has stopped");

}

} ;

}

}


interface Car{

public void go();


}
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380


Ok so in your Stop() method you have another anon class defined - having the new definition of Stop() method within Stop method itself.

So well I think that the control is going into Ferrari's existing Stop method (1) but after defintion - nothing happens. You need to call the newly defined Stop() method within existing Stop method . So --




I think the (2) should do. It should not become a recursive call - which it could have - had the class not been redefined anonymously within this method.


-Cheers,
Shivani.


/** Code speaks louder than words */
Philani Dlamini
Greenhorn

Joined: Apr 07, 2005
Posts: 23
Hi Shivani

Tnx for reading through that long un-indented code snippet.
Deep down, i guess i was hoping the call would be recursive.

call at (2) is neccessary then...tnx
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

Originally posted by Philani Dlamini:
Hi Shivani

Tnx for reading through that long un-indented code snippet.
Deep down, i guess i was hoping the call would be recursive.

call at (2) is neccessary then...tnx


I am not a fan of recursion for no reason -- but I guess it the best way to learn it.

Your inner's inner class is running... but remember it is not calling the stop method from the constructor, it is calling the go() method. So... in your stop methods inner class, if you override the go() method instead, it should work. It is kinda awkward, but it should work.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Anonymous Inner Class Problem
 
Similar Threads
Is there a way to instantiate anonymous inner class?
Inner Classes
instanceof Comparison
Inner Classes Mock question doubt 3
Interface