• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Why a final instance variable needs to be initialized ?  RSS feed

 
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.
 
Ranch Hand
Posts: 448
1
IntelliJ IDE 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.
 
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??
 
Sheriff
Posts: 21459
96
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.
 
Marshal
Posts: 60155
188
  • 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
Marshal
Posts: 60155
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!