Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why a final instance variable needs to be initialized ?

 
goel Ashish
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Why a final instance variable needs to be initialized if we dont even use it in our code? why dont compiler provides a default value to it?
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.
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A final instance variable is meant to hold a constant value (value cannot be changed). Now if the compiler assigns a default value then how can it be changed to hold something meaningful?

Cheers,
Raj.
 
Federico Cardelle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
goel Ashish
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's OK, because it will then always have a value after the object is constructed. Just make sure you don't use it inside the constructor before it's initialized.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
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.
 
Federico Cardelle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
goel Ashish
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ all Thnks for your help.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic