• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonymous Class Implementation

 
Neha Mohit
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...)
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Ernsest, i really appreciate ur prompt reponses.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic