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
 
Ad

Advertisements

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.
 
Ad

Advertisements

R

Rick Genter

I would use a public static final int:

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

Rick
 

Top