Tuesday, September 21, 2010

Const in java (Final)

Defining the constants:
'public static final' variables are constant.
The naming convention for static final variables is to have them in upper case and separate two words with an underscore. For example:
static final int NUMBER_OF_MONTHS = 12;
static final float PI = (float22 7;
If you want to make a static final variable accessible from outside the class, you can make it public too:
public static final int NUMBER_OF_MONTHS = 12;
public static final float PI = (float22 7;
 
Note:
  1. Using the final keyword to declare a variable.
  2. The final keyword specifies that the value of a variable is final and cannot be changed.
  3. It is a convention in Java to write constants in uppercase letters.
  4. Sometimes we have to use enums instead of these simple constants for better readability.
 

No comments:

Post a Comment

Chitika