• 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

Help please...

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can sombody please help me in understanding this code?
class A
{
class B
{
public void method2() { }
}
public static void main(String[]args)
{
new A().new B(){ public void method2() { } }; <======= What the hell is this line doing???
}
}
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's creating an anonymous class, which happens to
extends the inner class B.
Just compile it and you will see A$B.class and A$1.class
are created. A$1.class is the anonymous class.
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hm, well if the code is intended is make your eyes bang together, I suppose it's accomplished that. What it does that is useful is another debate.
class B is an inner class, defined within the definition of class A. It has a method called method2 that has zero or more null statements in it.
In the main(), class A is contructed and its implicit result is used as a reference to construct inner class B. The open brace following 'new B()' introduces an anonymous inner class, which is a way to extend B without giving the subclass a name. In this anonymous inner class, method2() is overridden by adding zero or more null statements to its parent form.
That's it; once the anonymous inner class is written, main() exits shortly thereafter.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You guys are faster on the draw than I am.

[This message has been edited by Marilyn deQueiroz (edited October 20, 2001).]
 
Barakeh Jev
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot! I got it now.
 
You save more money with a clothesline than dozens of light bulb purchases. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic