• 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

Declare A Constant Within a Method

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Believe it or not, I'm actually having trouble finding information on this. I would like to declare a constant within a method. For example, let's say that my "addRecord" method uses the field name "empleid" many times. Typically in other programming languages, I would declare a constant within the "addRecord" method because...
a) I prefer sticking values in variables whenever it's beneficial
b) I don't want myself (or anyone else later) to accidentally change this value half-way through the method.
How would you properly declare a constant within a method? I tried the following:

But I got an "illegal start of expression" error. Thanks in advance for any help you can give me.
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tom, if I understand you correctly, just use
final int SOMEVAL = 123;
as the static keyword cannot be used inside a method in Java.
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Get rid of "static".
"final" is all you need to make the variable read-only.
 
Tom Purl
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! That was what I suspected but I wasn't 100% sure and I couldn't find any information on the topic.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic