• 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 with static

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code works fine


But this gives an error
cannot assign a value to final variable x


Can't get the reason why marking it static gives this error regarding final variable.
 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Angela

Non-Static(instance) variables are initialized at the time of object creation.so at first one u r not getting error


But in static members, these are initialized at Class loading time itself
So in "static final int x;" x value must known to the compiler at the time of compilation it self.

i hope it will help u
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the second case variable x is a class variable. It should be initialized at the time of definition. We can initialize a final instance variable inside the constructor but not class variable. So you will get a compile time error if you try to initialize a final class variable inside a constructor.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a case where a final static member is initialized at class loading time with a value not known at compile time:

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sundar Murthi:
Hi Angela

Non-Static(instance) variables are initialized at the time of object creation.so at first one u r not getting error


But in static members, these are initialized at Class loading time itself
So in "static final int x;" x value must known to the compiler at the time of compilation it self.

i hope it will help u



Going along with Barry's example, the value of static variable is still assigned prior to the class instantiation (it is determined when the class loads since all static block initializers are executed at the time the class loads). Thus, it does not matter that the actual value is known at compile time for the static variable, just that it is assigned when the class loads and before object instantiation (when you class constructor method executes).
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic