View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide

Declare a constant

See Definition of a Constant

To declare a constant in Java you usually use:

public static final type CONSTANT_NAME = value;

Constants are usually public so that object of other classes have access to them. They are usually static so that there is only one copy of the constant no matter how many objects exist. They must be final so that the value won't change. The convention for naming a constant is to use all uppercase letters and underscores between words. You should assign a value when you declare a constant.

Examples:

public static final double PROB_OF_MOVING = 1.0 / 7;
public static final int NUM_STEPS = 10;
public static final String DEFAULT_GREETING = "Hello";

Links to this Page