• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Why a final instance variable needs to be initialized ?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 479
1
IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22895
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 81618
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ all Thnks for your help.
 
Campbell Ritchie
Marshal
Posts: 81618
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
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
reply
    Bookmark Topic Watch Topic
  • New Topic