aspose file tools*
The moose likes Beginning Java and the fly likes Why a final instance variable needs to be initialized ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why a final instance variable needs to be initialized ?" Watch "Why a final instance variable needs to be initialized ?" New topic
Author

Why a final instance variable needs to be initialized ?

goel Ashish
Greenhorn

Joined: May 14, 2011
Posts: 21
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

Joined: Mar 02, 2005
Posts: 443
    
    1

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

Joined: Jul 26, 2011
Posts: 26
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

Joined: May 14, 2011
Posts: 21
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

Joined: Oct 27, 2005
Posts: 19759
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
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

Joined: Jul 26, 2011
Posts: 26
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

Joined: May 14, 2011
Posts: 21
@ all Thnks for your help.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why a final instance variable needs to be initialized ?