Why a final instance variable needs to be initialized ?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
2. Why the following code prints 10 ?
because afaik constructor is the first thing that gets executed in a class before the initialization of instance variables.
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
Cheers,
Raj.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
goel Ashish wrote:
because afaik constructor is the first thing that gets executed in a class before the initialization of instance variables.
The order of execution is
1.the constructor of the superclass (in this case is is Object() and it does nothing)
2.initialization of instance variables
3.the code in the constructor
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Federico Cardelle wrote:
The order of execution is
1.the constructor of the superclass (in this case is is Object() and it does nothing)
2.initialization of instance variables
3.the code in the constructor
If it is like that then what happens when we dont initialize a final variable during declaration and initialize it inside a constructor??
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
The final variable is initialised by the constructor. In my opinion, that is the correct way to do it. A final field must be initialised before the constructor completes.goel Ashish wrote: . . . If it is like that then what happens when we dont initialize a final variable during declaration and initialize it inside a constructor??
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
goel Ashish wrote:
Federico Cardelle wrote:
The order of execution is
1.the constructor of the superclass (in this case is is Object() and it does nothing)
2.initialization of instance variables
3.the code in the constructor
If it is like that then what happens when we dont initialize a final variable during declaration and initialize it inside a constructor??
I think that I remember that final fields are not initialized to its default value, if they aren't assigned a explicit initialization value when they are declared (while non final fields are). But I can't find where I read it.
This way they are "blank finals" until they are initialized in the constructor.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
| Joel Salatin has signs on his property that say "Trespassers will be Impressed!" Impressive tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |









