Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Anonymous Class Implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Anonymous Class Implementation" Watch "Anonymous Class Implementation" New topic
Author

Anonymous Class Implementation

Neha Mohit
Ranch Hand

Joined: Apr 25, 2006
Posts: 87
hi , i am trying to implement an anonymous class but there is something which my compiler doesn't like, please help me

class Food
{
Popcorn p= new Popcorn()
{
public void pop()
{
System.out.println("A n o n y m o u s P o p c o r n");
};

};
public static void main(String[] args)
{
Food f = new Food();
f.p.pop();
}
}

Anonymous.java:3: cannot find symbol
symbol : class Popcorn
location: class Food
Popcorn p= new Popcorn()
^
Anonymous.java:3: cannot find symbol
symbol : class Popcorn
location: class Food
Popcorn p= new Popcorn()
^

Thanks in advance
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Popcorn needs to be in your import statements if you are going to use it (BTW - I don't see any anonymous class defined in the code you posted...)


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Originally posted by Paul Sturrock:
I don't see any anonymous class defined in the code you posted...


No, it's there -- it's an anonymous subclass of "Popcorn".

My question to the original poster is: do you understand that you're defining an anonymous class that is a subclass of "Popcorn", which must already exist?
[ May 11, 2006: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


No, it's there -- it's an anonymous subclass of "Popcorn".

Doh! Think I was fooled by the indentation (or lack of).
Neha Mohit
Ranch Hand

Joined: Apr 25, 2006
Posts: 87
sorry all i know about anoymous class is that they dont have name,and they have one instance per object of class in which they are present.

please help me !!

regards
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

An anonymous class can't usefully define its own method signatures; it can only extend an existing class or implement an existing interface. It's a good way, for example, to define a tiny Thread class:



The anonymous class is a subclass of Thread. We override Thread's "run" method to do something useful, then treat "t" as you would any other Thread.
Neha Mohit
Ranch Hand

Joined: Apr 25, 2006
Posts: 87
Originally posted by Neha Mohit:
,and they have one instance per object of class in which they are present.




is this correct
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Yes, the only way to define an anonymous class is by creating an instance; the only way to create an instance is to define the class. Therefore, there is always one, but only one, instance of the class.
Neha Mohit
Ranch Hand

Joined: Apr 25, 2006
Posts: 87
thanks Ernsest, i really appreciate ur prompt reponses.
 
 
subject: Anonymous Class Implementation
 
Similar Threads
anonymous class
Anonymous inner classes
K&B Anonymous Inner Class.
How to access Inner Anonymous Class
Anonyms Inner Class ????