Newbie question - Java form of C #defines for constants.

J

John Leonard

I'm working on a program where I want the string constants "white" and
"black" to have pre-determined global values. In C I would have just used a
#define statement like:

#define WHITE 0
#define BLACK 1

What is the appropriate way to achieve the same effect in Java?

John Leonard
 
C

Christophe Vanfleteren

John said:
I'm working on a program where I want the string constants "white" and
"black" to have pre-determined global values. In C I would have just used
a
#define statement like:

#define WHITE 0
#define BLACK 1

What is the appropriate way to achieve the same effect in Java?

John Leonard

The closest you can get is using static fields
Example:

public class Constants {

public static final String WHITE = "value";
}

Then use it like this
....
System.out.println(Constants.WHITE);


You can also emulate enumerations (real enumerations will come with jdk1.5).

See
http://developer.java.sun.com/developer/JDCTechTips/2001/tt0807.html#tip2
for that.
 
R

Rick Genter

I would use a public static final int:

public static final int WHITE = 0;
public static final int BLACK = 1;

Rick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top