3
public class A{
}

A a = new A(){{
    final int x = 1; // IT HAS TO BE FINAL HERE. WHY?
}};

A aa = new A(){
    int x = 1; // THIS NEED NOT BE FINAL. WHY?
    final int y = 1; // STILL FINAL IS ALLOWED HERE. WHY?
    public int getX(){
        return x;
    }
};

Can somebody please answer the questions mentioned inside the snippet?

Thanks

5 Answers 5

4

The outer set of {} declares an anonymous subclass

The inner set declares an initialization block within that subclass.

With the following example, it becomes easier to understand what's going on:

List<String> strings = new ArrayList<String>() {{
    add("string");
    add("another string");
}};

You basically say: I want a subclass of List, which calls method add at initialization.

It's similar to:

List<String> strings = new ArrayList<String>();
strings.add("string");
strings.add("another string");
Sign up to request clarification or add additional context in comments.

1 Comment

The inner {} is an initialization block, but is not static. If it were static, it would have the keyword static before it - but you can't have a static initializer inside an anonymous inner class.
3
A a = new A(){{
    final int x = 1; // IT HAS TO BE FINAL HERE. WHY?

It needn't.

The difference between the two is that in the first case, you're writing the code used to initialize each object in the double braces. That x is its local variable (doesn't have anything to do with objects of class A).

In the second case, you're defining the classes body. x would be its member variable. If it were static, its class variable. If final a (basically) constant.

2 Comments

Is there any Java book that explains nitty gritty details of Java Language?
@ajay yes, it's called the Java Language Specification.
2

I wonder, why would you want to use the first version of the code (the one with {{}}). The x variable declared inside it's not useful at all, it's local only to the block scope in which it was defined, and being inside an anonymous class, you won't be able to reference it anywhere in the code.

And anyway, the first version of x declaration does not need to be final, it compiles just as fine, with or without final.

1 Comment

You would use the double-brace version because it's an idiom: c2.com/cgi/wiki?DoubleBraceInitialization Personally I don't think it's a very good one; it masks a surprising amount of overhead.
1

In your first example the inner braces create something called an instance initializer. It's an obscure way to initialize variables and call instance methods when an object is constructed. So the first example creates an anonymous subclass of A and creates a local variable within the scope of the initializer. It doesn't have to be final.

In the second example you're creating an anonymous subclass of A but not creating an instance initializer, the variables you create are instance variables of the anonymous subclass.

Comments

1

The first instance A a = new A(){{... has an initialisation block inside the declaration. Another way of writing it would be like this:

A a = new A()
{
    {
        final int x = 1; // This is inside an initialisation block
    }        
};

Changing the formatting makes it a bit more obvious.

4 Comments

Why does the static initialziation block not allow putting static final int x = 1 there?
the first instance doesn't have a static initialization block, it has an instance initialization block. you can put stuff in the block that references the instance (which is how the double-brace initialization trick works).
The inner {} is an initialization block, but is not static. If it were static, it would have the keyword static before it - but you can't have a static initializer inside an anonymous inner class.
@ajay: the variables declared there are local variables. so just like you can't have a static variable defined within a method, it can't go here either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.