Java Constants

Java Constants

  • It is common convention to name the constants only in uppercase.

Java really constant

  • To define really constant it is important to use the combination of static and final modifiers.
  • static means that the variable belongs to the class it is defined in and is not contained in any object created out of the class.
  • final means that the variable cannot be changed.
    Really constant example:
    public class anyClass {
     static final int SIMPLE = 0, ONE_SPRING = 1, TWO_SPRING = 2;
    ...
    }
    

Java enum as constant

  • You can define enum constant type inside a class or outside, as you do with classes or interfaces.
    enum constant inside a class:
    public class AnyClass {
    
      enum Weekday {SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
        THURSDAY, FRIDAY, SATURDAY};
    
      public static void main(String[] args) {
        Weekday day = Weekday.MONDAY; // usage 
        System.out.println(day); // this prints: MONDAY
      }
    }
    
    enum constant outside a class:
    enum Weekday {
        SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
        THURSDAY, FRIDAY, SATURDAY 
    }
    public class AnyClass {
      public static void main(String[] args) {
        Weekday day = Weekday.MONDAY; // usage 
        System.out.println(day); // this prints: Monday
      }
    }
    
  • An enum type is like a class or interface reference type. You can define constructors, methods and variables in a java enum. This makes enum very powerful..
  • All enums implicitly extend java.lang.Enum with methods it contains.
  • It is nice to know that the java compiler automatically also generates a static values() method that returns an array containing all of the values of the enum in the order they are declared.
    enum constant with constructor and methods:
    enum Margin {
      TOP(4), RIGHT(2), BOTTOM(4), LEFT(4);  // must bee defined first with an ending ;
      private int pos;
    
      private Margin(int pos) { // constructor must bee private or without any modifier
        this.pos = pos;
      }
    
      public int getPos() {  // Only get methods as you cannot change a constant.
        return pos;
      }
      
    }
    public class AnyClass {
      public static void main(String[] args) {
        int count=1;
        // we are here using the generated static values() method
        // which returns an array of elements.
        for (Margin m: Margin.values())    {  
          System.out.printf("Margin %s = %s\n", m,  m.getPos());
        }
      }
    }
    

Spesial Java constants to use

  • In your code, you can use special sequence characters notation.
    Some of these special sequence characters notation are:
    ...
    \n	      Newline (\u000A)
    \t	      Tab (\u0009)
    \b	      Backspace (\u0008)
    \r	      Return (\u000D)
    \f	      Form feed (\u000C)
    \\	      Backslash itself (\u005C)
    \'	      Single quote (\u0027)
    \"	      Double quote (\u0022)
    \ddd	      An octal char, with each d being an octal digit (07)
    \uxxxx 	      A Unicode char, with each x being a hex digit (09, af, AF)
    ...
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.