• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Final Variables

 
Greenhorn
Posts: 5
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi...!!! check this program:


Acc to me(with ref to the above code) Final variables are not assigned default values ever but i hv not read this statement in any book i hv read abt java......
Can somebody please clarify this fact!!!....
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

what you have read is correct

from JLS


A blank final is a final variable whose declaration lacks an initializer.
Once a final variable has been assigned, it always contains the same value



in your case the blank variable is an instance variable, so you can only access the variable after the constructor execution. so it make sense to initialize i value in constructor.
remember if the variable i is static, then you need a static initializer instead of constructor to initialize i value

hth
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also
the final instance variables area needed to be given value in the same line or in every constructor
this is because
before the constructor runs, the default values are assigned to the instance variables after the super class constructor has completed execution
so when we write
final int i ;
then the value i = 0 is automatically given to i
 
karry sharma
Greenhorn
Posts: 5
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnx 4 the reply....i have checked out the JLS and now i am very sure of this fact.....
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Karthik please check your private messages for some administrative matter.

Thnx 4 the reply


Read this...
 
Maybe he went home and went to bed. And took this tiny ad with him:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic