Wednesday, April 30, 2008

Use globally defined constants

You can use globally defined constants by defining them inside a class and referencing themwithout instantiation.

public class CONSTANT {

public static final integer SUCCESS = 1;
public static final integer FAILURE = -1;
public static final integer NOTFOUND = 0;

}

Since the members of the class are defined as "static", there is no need to instantiate the class. To use a constant, simply use CONSTANT.[constant name]

if (myMethod()==CONSTANT.SUCCESS) {
...;
}
else {
...;
}

For details: Java Tips

No comments: