• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

what this code is called

 
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry to ask question on syntax, I tried to google but I did not get the clear answer. If you can help please help me.

What is this syntax called, what is the use of it.



 
Marshal
Posts: 80943
522
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like the body of an anonymous class. Look in the directory where all your XYZ.class files are, and you will probably find one called PQR$1.class or similar.
 
Sheriff
Posts: 17735
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The right hand side of that assignment statement is an anonymous class, formally defined in the Java Language Specification as an UnqualifiedClassInstanceCreationExpression: https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.9

UnqualifiedClassInstanceCreationExpression:
new [TypeArguments] ClassOrInterfaceTypeToInstantiate ( [ArgumentList] ) [ClassBody]



The purpose of the block is to provide a one-off implementation of a method or several methods of the declared class type. In your example, you're basically telling Java to "Create an instance of a new subclass of TreeMap with the following implementations of these methods (defined inside the block)."
 
Bartender
Posts: 15743
368
  • Likes 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an "anonymous class declaration". The block is used to define the class body of an anonymous custom type that extends TreeMap. It's as if you had written the following code:

The important difference is that the class that you declare using an anonymous class declaration doesn't have a name, and a new instance of that class is created instantly.

Anonymous classes are used when you don't need to refer to the custom type outside of the method, and you only need one instance per method call. They're usually not used to extend complete classes like you did with TreeMap, but instead they are usually used to implement interfaces or abstract classes. The most common use case are event listeners:

Many interfaces have only one abstract method, and since Java 8 you can implement them using a lambda expression instead of an anonymous class declaration:

However, anonymous class declarations are still useful if an interface consists of more than one abstract method, or when you want to implement an abstract class, like I did above with MouseAdapter.

Some people abuse anonymous class declarations to add some initialization code to the new instance:

This syntax looks super weird, but it's really as if you wrote the following class:

Do NOT do this. It's poor, lazy programming. Instead, do this:
 
Sheriff
Posts: 28430
103
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Do NOT do this. It's poor, lazy programming. Instead, do this:



This is certainly true since Java 9, when Map.of arrived. So yeah, you should be using Map.of in new code now. But you're likely to find the double-brace hack in code which was written before then.
 
jacob deiter
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you!!!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic