Mitja Kancler wrote:I belive that the variable name is not instance variable, so it must be local. Where does it get created? Is it created on the stack even thou there was no method call?
You are correct! The variable
name is indeed not an instance variable, nor a class (static) variable. It is a local variable and its scope is the instance initializer block. That means that the variable will exist only during the execution of this instance initializer block. So you can have multiple instance initializer blocks defining a variable
name and the code will still compile
Once the instance initializer block is executed, the variable
name will be created on the stack (and the object it refers to, will be placed on the heap).
Finally, keep in mind that a local variable
must be initialized before it is used/accessed. So the code snippet from your previous post will not compile
In
this topic you'll find an overview about the initialization for class (static), instance and local variables (even when they are marked as
final).
Hope it helps!
Kind regards,
Roel