Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declare A Constant Within a Method

 
Tom Purl
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • 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.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • 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.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic