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

final static

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

When I add static modifier to the final variable a then I am getting compilation error and when it gets initialised the error goes off and runs by taking out the constructor and is fine(Cann't change the final value).
Could any one help me in understanding the behaviour of final with static?
Thanks,
Anna S Iyer.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static final vars MUST be initialized either in declaration or in one of static initialization blocks.NOT in constructors.The compiler does not wait until the construction of the object.
But instance final vars may be initialized in the constructor. If it is initialized in constructor, ALL CONSTRUCTORS MUST initialize the instance final var.
In both cases once it is initialized we can not reassign a new value to them
:
there u go.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not just the static that counts, but also the final.

  • static = class/non-instance member/variable
  • final = its value cannot be changed except....
  • [non static] final means you cannot change the value except within the constructors
  • static final means you cannot change the value (including within constructors) except within the static initialization block which is executed when the class is loaded.
    So if you want to make a static final variable then
    you initialize it either along with the declaration or within
    the static block.
    Add the following after the declaration

    Hope this would help
  •  
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic