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
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
xis its local variable (doesn’t have anything to do with objects of classA).In the second case, you’re defining the classes body.
xwould be its member variable. If it werestatic, its class variable. Iffinala (basically) constant.