Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Anonymous Class Implementation

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...)
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Ernsest, i really appreciate ur prompt reponses.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic