• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Anonymous Inner Classes doubt

 
Ranch Hand
Posts: 114
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Popcorn
{
public void pop()
{
System.out.println("Popcorn");
}
}



public class AnonymousinnerClasses
{

Popcorn p = new Popcorn(){public void pop()
{
System.out.println("Anonymous popcorn");
}
};

}
how do you instantiate an anonymous inner class(i.e.how to write the main method to run this program).
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your example, all you need to do is create an instance of AnonymousinnerClasses so that the instance variable p gets valued. At that point, p will reference an instance of the anonymous class.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepaks Deshpande:
class Popcorn
{
public void pop()
{
System.out.println("Popcorn");
}
}



public class AnonymousinnerClasses
{

Popcorn p = new Popcorn(){public void pop()
{
System.out.println("Anonymous popcorn");
}
};

}
how do you instantiate an anonymous inner class(i.e.how to write the main method to run this program).

 
yogi maheshnath
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
-------------------------------------------------------------------
class Popcorn
{
public void pop()
{
System.out.println("Popcorn");
}
}

class AnonymousinnerClasses
{

Popcorn p = new Popcorn(){public void pop()
{
System.out.println("Anonymous popcorn");
}
};
public static void main(String args[])

{
AnonymousinnerClasses a=new AnonymousinnerClasses();
a.p.pop();
}
}
--------------------------------------------------------------------
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Mahesh,

Thank you for your post.Actually I am waiting for the reply of my post as me too posted same question.
 
Deepaks Deshpande
Ranch Hand
Posts: 114
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by yogi maheshnath:
[QB][/QB]



i think we can do it like this also..
public void display()
{
p.pop();
}

public static void main(String[] args)
{
AnonymousinnerClasses pa = new AnonymousinnerClasses();
pa.display();
}
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't really see why you have a problem with your question:

how do you instantiate an anonymous inner class(i.e.how to write the main method to run this program).



So I just present one possible solution:

But you don't really need this extra class, you could paste the main method also into one of your classes.


Yours,
Bu.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic