This week's book giveaway is in the Agile forum.
We're giving away four copies of Fixing your Scrum and have Ryan Ripley & Todd Miller on-line!
See this thread for details.
Win a copy of Fixing your Scrum this week in the Agile 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

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: 73738
332
  • 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: 16564
272
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)."
 
Saloon Keeper
Posts: 13174
285
  • 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:
 
Marshal
Posts: 26745
81
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!!!
 
I knew that guy would be trouble! Thanks tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic