File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Declare A Constant Within a Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Declare A Constant Within a Method" Watch "Declare A Constant Within a Method" New topic
Author

Declare A Constant Within a Method

Tom Purl
Ranch Hand

Joined: May 24, 2002
Posts: 104
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.


Tom Purl<br />SCJP 1.4
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
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

Joined: Jun 06, 2002
Posts: 1056
Get rid of "static".
"final" is all you need to make the variable read-only.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Tom Purl
Ranch Hand

Joined: May 24, 2002
Posts: 104
Thanks! That was what I suspected but I wasn't 100% sure and I couldn't find any information on the topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Declare A Constant Within a Method