Curly braces in new expression? (e.g. new MyClass() { ... })
This is the syntax for creating an instance of anonymous class that extends Handler
. This is part of Java.
Code with curly braces in/after new?
The initialValue() method of ThreadLocal is just a way to construct a ThreadLocal holding a value other than null.
Edit: Oh, I see that's not what you're asking about. What you have there is the same as if you did:
public class MyOwnThreadLocal extends ThreadLocal {
public Connection initialValue() {
return DriverManager.getConnection(DB_URL);
}
}
Except your version doesn't require a completely separate class definition--hence it's called an "anonymous class".
What are the advantages of list initialization (using curly braces)?
Basically copying and pasting from Bjarne Stroustrup's "The C++ Programming Language 4th Edition":
List initialization does not allow narrowing (§iso.8.5.4). That is:
- An integer cannot be converted to another integer that cannot hold its value. For example, char
to int is allowed, but not int to char. - A floating-point value cannot be converted to another floating-point type that cannot hold its
value. For example, float to double is allowed, but not double to float. - A floating-point value cannot be converted to an integer type.
- An integer value cannot be converted to a floating-point type.
Example:
void fun(double val, int val2) {
int x2 = val; // if val == 7.9, x2 becomes 7 (bad)
char c2 = val2; // if val2 == 1025, c2 becomes 1 (bad)
int x3 {val}; // error: possible truncation (good)
char c3 {val2}; // error: possible narrowing (good)
char c4 {24}; // OK: 24 can be represented exactly as a char (good)
char c5 {264}; // error (assuming 8-bit chars): 264 cannot be
// represented as a char (good)
int x4 {2.0}; // error: no double to int value conversion (good)
}
The only situation where = is preferred over {} is when using auto
keyword to get the type determined by the initializer.
Example:
auto z1 {99}; // z1 is an int
auto z2 = {99}; // z2 is std::initializer_list<int>
auto z3 = 99; // z3 is an int
Conclusion
Prefer {} initialization over alternatives unless you have a strong reason not to.
Java programming language syntax
The code following curly brackets is syntax for an anonymous class. This is similar to a lambda expression you may have seen in C++11, except that it defines an entire class versus just one function (so an anonymous class instead of an anonymous function).
java just curly braces
It's a code block. The variables declared in there are not visible in the upper block (method body outside of these curlies), i.e. they have a more limited scope.
Related Topics
The Activity Must Be Exported or Contain an Intent-Filter
Online Radio Streaming App for Android
How to Get Selected Xls File Path from Uri for Sdk 17 or Below for Android
Manifest Merger Failed:Uses-Sdk:Minsdkversion 8 Cannot Be Smaller
Android - Viewrootimpl$Calledfromwrongthreadexception
How to Get Java Output with Ruby
Pyspark: Exception: Java Gateway Process Exited Before Sending the Driver Its Port Number
Setting the Default Font of Swing Program
How to Get String Response from Retrofit2
What Is the Simplest Way to Reverse an Arraylist
Xmpp with Java Asmack Library Supporting X-Facebook-Platform
Getlocationonscreen() VS Getlocationinwindow()
Android 6.0 (Marshmallow): How to Play Midi Notes
Why Does My Compare Method Throw Exception -- Comparison Method Violates Its General Contract!