• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

final var

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anybody pls explain this...
all member variables are automatically initialised to a default value if not explicitly initialised....accr to whatever datatype....
ex:
int i; // automatically initialised.
but if a member variable is suppose final....
final int i;........it is not initialised why?
compiler says it has to be initialised.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will have to assign a value to final int either at the place where it is declared or in one of the constructors.
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think a final variable has to be declared atleast before any constructor, if not at the time of declaration.
--Farooq
 
srinivas bolloju
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my question is why final var should be instantiated even though it is a class level var? while it is not for other class level vars?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,, for the first time ,, javaranch is very helpful,
The final variable is inlined at compile time .
Hope my answer is correct,,cory
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Srinivas,
A final instance field has to be initialized when it is declared, in an initializer block or in every constructor.
When you use the 'final' modifier your saying 'this field can only have one value for the life of the object' so the rules compel you to assign that value when the object is created.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
srinivas bolloju
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got it ,thanx everybody
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We meet again Srinivas . final is final . Once defined u can't change it in u'r code . This means that nowhere in u'r code u can assign a value to it . So it makes sense that u gotta initialize it upon declaration .
A final variable is basically a constant u could initialize it with an expression .
final int PI = 3.14 ;//valid
final int PI = 22/7 ;//valid
final int PI = x * 12/7 ;//valid
10.30am & i still haven't slept . Insomnia i guess !
reply
    Bookmark Topic Watch Topic
  • New Topic