Syntax for separating variable declaration and anonymous class instantiation in android/java

WWH

According to Android Studio, instead of doing this:

private BluetoothAdapter.LeScanCallback mCallback = new BluetoothAdapter.LeScanCallback() {

        @Override
        public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {

        }
    };

I can do this:

private BluetoothAdapter.LeScanCallback mCallback;

{
    mCallback = new BluetoothAdapter.LeScanCallback() {

        @Override
        public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {

        }
    };
}

I think the second syntax option is much prettier, but I don't understand why it requires curly braces around the anonymous class instantiation. My understanding is that curly braces locally scope the enclosed code, which doesn't make a whole lot of sense to me. What am I missing?

Piotr Praszmo

Those brackets around field initialization are called initializer block. Everything you put inside is executed with constructor. You can execute any code within it:

class Main {
    int a = 1;
    int b;

    {
        b = 1;
        System.out.println("Hello World!");
    }
}

I prefer the first approach. Initializer blocks introduce unnecessary complexity and confusion. For example this is a compile error:

Object a = b.toString();
Object b = "";

While this will fail at runtime:

Object c;
Object d;

{
    d = c.toString();
    c = "";
}

It becomes even more complex when you add regular constructors and superclasses to the mix.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Instance Variable Declaration and instantiation

From Dev

Statements in anonymous class declaration

From Dev

Javascript syntax: variable declaration with "<<" or ">>"

From Dev

variable declaration conditional syntax

From Dev

Anonymous function, declaration and expression syntax understanding

From Dev

Multiple instantiation of class in the same variable

From Dev

"syntax error, unfinished class declaration"

From Dev

Class - variable declaration

From Dev

Class - variable declaration

From Dev

Impact of separating class definition from declaration on program size

From Dev

What is the difference between these two declaration of anonymous class

From Dev

Clojure using let variable declaration within its own instantiation?

From Dev

Clojure using let variable declaration within its own instantiation?

From Dev

"class VARIABLE" in declaration of variable for template class?

From Dev

"class VARIABLE" in declaration of variable for template class?

From Dev

Python: What is the sense of class instantiation without variable

From Dev

Syntax for invoking multiple methods of an anonymous class in Java?

From Dev

Java strange syntax - (Anonymous sub-class)

From Dev

What is the class name for in this variable declaration?

From Dev

Variable declaration inside of class in Swift

From Dev

What is the class name for in this variable declaration?

From Dev

What is this parenthesis enclosed variable declaration syntax in Go?

From Dev

Python variable names __ and _ and @property syntax with declaration location

From Dev

Incorrect syntax near ')'. in variable declaration of sql

From Dev

What does this syntax mean in the declaration of an abstract class?

From Dev

Constructor and Destructor Declaration Syntax with Template Class

From Dev

OpenCV - Ptr syntax and class definition / declaration - confusion?

From Dev

Declaration of variables in the class extension (anonymous category) vs Implementation

From Dev

Separating definition/instantiation of template classes without 'extern'

Related Related

  1. 1

    Instance Variable Declaration and instantiation

  2. 2

    Statements in anonymous class declaration

  3. 3

    Javascript syntax: variable declaration with "<<" or ">>"

  4. 4

    variable declaration conditional syntax

  5. 5

    Anonymous function, declaration and expression syntax understanding

  6. 6

    Multiple instantiation of class in the same variable

  7. 7

    "syntax error, unfinished class declaration"

  8. 8

    Class - variable declaration

  9. 9

    Class - variable declaration

  10. 10

    Impact of separating class definition from declaration on program size

  11. 11

    What is the difference between these two declaration of anonymous class

  12. 12

    Clojure using let variable declaration within its own instantiation?

  13. 13

    Clojure using let variable declaration within its own instantiation?

  14. 14

    "class VARIABLE" in declaration of variable for template class?

  15. 15

    "class VARIABLE" in declaration of variable for template class?

  16. 16

    Python: What is the sense of class instantiation without variable

  17. 17

    Syntax for invoking multiple methods of an anonymous class in Java?

  18. 18

    Java strange syntax - (Anonymous sub-class)

  19. 19

    What is the class name for in this variable declaration?

  20. 20

    Variable declaration inside of class in Swift

  21. 21

    What is the class name for in this variable declaration?

  22. 22

    What is this parenthesis enclosed variable declaration syntax in Go?

  23. 23

    Python variable names __ and _ and @property syntax with declaration location

  24. 24

    Incorrect syntax near ')'. in variable declaration of sql

  25. 25

    What does this syntax mean in the declaration of an abstract class?

  26. 26

    Constructor and Destructor Declaration Syntax with Template Class

  27. 27

    OpenCV - Ptr syntax and class definition / declaration - confusion?

  28. 28

    Declaration of variables in the class extension (anonymous category) vs Implementation

  29. 29

    Separating definition/instantiation of template classes without 'extern'

HotTag

Archive