• 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

Strange line of code

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi,

In the code above, what does this line do?

I am confused about {}!!! Is it going to create an ANONYMOUS SUBCLASS of Bar and execute the method go() of this subclass?

Thank you very much.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swerrgy Smith wrote:Is it going to create an ANONYMOUS SUBCLASS of Bar and execute the method go() of this subclass?


Yes, spot on.

Try modifying the go() method in Bar to print out getClass().getName(), and you'll see the name of the class that actually gets executed. It'll probably be something like "Foo$1", which is how anonymous classes get named.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(new Bar() {}).go();
this line simply makes the anonymous class which extends the Bar class and the object this anonymous class call the method of our super class
so print foobarhi foo prints when we make the object of foo class and bar prints when we make the object of subclass or anonymous class of bar class and at last hi prints..
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:Yes, spot on.


@Swerrgy: Yes. And well done for working it out.

However, this looks like SCJP code to me; and it's worth remembering that it is only really designed to test your knowledge of the compiler. Don't expect to ever see anything like it in real life. And if you do...RUN.

And there's LOTS of stuff in that exam that you should forget as soon as you've passed it.

Winston
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this one. Can you explain how the following works?

Some people even consider this a legitimate technique. They're wrong .
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It even has a name: double-brace initialization.

Why do you consider it wrong?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:It even has a name: double-brace initialization.

Why do you consider it wrong?




Perhaps, one reason could be ... if programmers ran amok with this technique, every time a collection needed to be instantiated, there would be class files all over place.

Henry
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:Why do you consider it wrong?


Because it's arcane; and, as every programmer knows: simplicity (not brevity) is the essence of lingerie.

It took me a minute or so to work that one out. Never seen it before. Nice one Matthew.

Winston
 
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:Never seen it before.


What, didn't you read this thread before you replied to it?

It is arcane to most of us, but it doesn't need to be. It's easy enough to grok once you've been exposed to it, IMO.

For completeness I would note that these days, I would use Google Guava to do this more nicely:

Or if you really do need it mutable:

I just wish Java had nice collection literals like other languages.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:It even has a name: double-brace initialization.

Why do you consider it wrong?



To be fair, "wrong" is an exaggeration. I'm not a big fan, though, for reasons others have given. And it does just seem a bit, well, hacky. I'd agree with Mike - I'd prefer a proper initialiser syntax supported by the language.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:I'd prefer a proper initialiser syntax supported by the language.



I agree. VFP (also VB and probably other languages) has the "WITH / ENDWITH" keywords which I used extensively. With proper indentation, nested WITH clauses are eminently readable and concise.

In Java, double-brace initialization is the nearest thing to that, but can only be applied at construction.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic