Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curly braces in "new" expression? (e.g. "new MyClass() { ... }")

What do the curly braces do there ?

handler1 = new Handler() {          public void handleMessage() {         } }; 

object = new Class_Name() {}; ? This syntax exists only on Android or Java also? And what is it called in Java? Thank for your helps.

like image 352
wanting252 Avatar asked May 06 '12 07:05

wanting252


People also ask

What does this {} mean in Python?

In languages like C curly braces ( {} ) are used to create program blocks used in flow control. In Python, curly braces are used to define a data structure called a dictionary (a key/value mapping), while white space indentation is used to define program blocks.

What is a curly braces in programming?

In programming, curly braces (the { and } characters) are used in a variety of ways. In C/C++, they are used to signify the start and end of a series of statements. In the following expression, everything between the { and } are executed if the variable mouseDOWNinText is true. See event loop.

What is the purpose of {} in Java?

It's called an initializer block and is invoked every time an instance of the class is created. The Java compiler copies initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors.


Video Answer


1 Answers

This is the syntax for creating an instance of anonymous class that extends Handler. This is part of Java.

like image 162
MByD Avatar answered Sep 22 '22 23:09

MByD