Win a copy of Head First Android this week in the Android forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

final variable assignment

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some one please tell me how can i take run time value for static final variable...my lecturer said first time assignment is possible for declared final variable but in my case it shows compile time error..I'm placing my program below with error message
Sample.java:11: cannot assign a value to final variable cname.
cname=args[0];
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sujesh Katri wrote:Some one please tell me how can i take run time value for static final variable...my lecturer said first time assignment is possible for declared final variable but in my case it shows compile time error..I'm placing my program below with error message
Sample.java:11: cannot assign a value to final variable cname.
cname=args[0];



A final static variable can only be assigned once -- during initialization of the variable or via a static initializer. Assignments from a static method isn't allowed, as methods can be called more than once.

Henry
 
Marshal
Posts: 74354
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can writeBut you cannot initialise a final static field in a constructor (that would only work if you had one instance) nor in a method (you can even call the main method several times: as here).

Avoid using multiple initialiser blocks because you can get confused about the order they are executed in.
 
Ranch Hand
Posts: 50
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sujesh,
For taking runtime value for a final static variable, here is a suggestion:




contents of file ./config/SampleProperty.properties
MYNAME="PALLAVI"

You can change the value of MYNAME each time and run.
 
reply
    Bookmark Topic Watch Topic
  • New Topic