Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
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
Tim Cooke
paul wheaton
Ron McLeod
Jeanne Boyarsky
Sheriffs:
Paul Clapham
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Programmer Certification (OCPJP)
Anonymous Inner class
Shiva Mohan
Ranch Hand
Posts: 486
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I am trying here to display "anonymous popcorn".
class Popcorn{ public void pop(){ System.out.println("popcorn"); } } class MyInner{//----------------------------outer class Popcorn p=new Popcorn(){//----------------------------anonymous inner class public void pop(){ System.out.println("Anonymous popcorn"); } }; } class Objective15{ public static void main(String[] args){ Popcorn myPopcorn=new Popcorn(); MyInner.Popcorn anon=myPopcorn.new Popcorn();//---------------Some error anon.pop(); } }
Please help me what i am doing here wrong.
wise owen
Ranch Hand
Posts: 2023
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Popcorn myPopcorn=new Popcorn();
You can use:
MyInner myPopcorn=new MyInner(); Popcorn anon=myPopcorn.p; anon.pop();
or
Popcorn anon=new MyInner().p; anon.pop();
[ May 11, 2006: Message edited by: wise owen ]
Shiva Mohan
Ranch Hand
Posts: 486
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Wise-Thank you very much and i got it.
Shiva Mohan
Ranch Hand
Posts: 486
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
I am working againg on Argument defined -Anonymous inner class and i coultn't figure out how to correct the error.Please help me.
class Cookable{ void cook(Foo f){ } } class Objective15{ Cookable c =new Cookable(); c.cook(new Foo() {//--------------line1 public void foof(){//-----------------------we need to overide abstract method here System.out.println("foofy"); }); } public static void main(String[] args){ Objective15 myObject=new Objective(); Objective15 inner=myObject.c; inner.foof(); }//---------------------------------line2 } interface Foo{ void foof(); }
how the line 1 is showing error? It seems like i have given correctly.
wise owen
Ranch Hand
Posts: 2023
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
interface Foo{ void foof(); } class Cookable{ public Foo f = null; void cook(Foo f){ this.f = f; } } public class Objective15 { Cookable c =new Cookable(); { c.cook(new Foo() { public void foof(){ System.out.println("foofy"); };}); } public static void main(String[] args) { Objective15 myObject=new Objective15(); Cookable inner=myObject.c; inner.f.foof(); } }
I had rewrited some of your codes to make it work. You can see where I had made changes.
[ May 11, 2006: Message edited by: wise owen ]
Shaliey Gowtham
Ranch Hand
Posts: 104
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Shiva,You missed the closing curly braces inside the parameter call and have added it outside the paranthesis.
Could you get it?
Blood pressure normal? What do I change to get "magnificent"? Maybe this tiny ad?
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Can u plz explain the anonymous inner classes??
K&B Anonymous Inner Class.
Accessing primitives in anonymous inner class
Anonymous inner classes
Anonymous inner class
More...