Home     Blog

How to Declare a Constant in Java

Java does not directly support constants. However, a static final variable is effectively a constant.

The static modifier causes the variable to be available without loading an instance of the class where it is defined.

The final modifier causes the variable to be unchangeable.

Naming Standards for Java Constants

Java constants are normally declared in ALL CAPS.

Underscores normally separate Words in Java constants.

Sample Java Constant Declaration

public class MAX_UNITS {
 public static final int MAX_UNITS = 25;

how to declare a constant in java How to Declare a Constant in Java

Vedios Related to Java Constant Declaration

VN:F [1.9.17_1161]
Rating: 10.0/10 (2 votes cast)
How to Declare a Constant in Java, 10.0 out of 10 based on 2 ratings
Follow Will.Spencer on

Comments (2)

 

  1. Jinal says:

    Is it necessary to write static while declaring constant?
    if we only prefix ‘final’ modifier, then doesn’t it mean the variable is constant?
    Why only final is not enough? Why static is required with final to make constants in java?
     

    VA:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
    • Will.Spencer says:

      You can skip ‘static’, but then your variable will only be available when you load an instance of the class where it is defined.  If you always load an instance of that class, you should be fine.

      VN:F [1.9.17_1161]
      Rating: 0.0/5 (0 votes cast)

Leave a Reply

Related Posts

  • Java vs. C++

    C++ supports pointers; Java does not (Java does have object references) C++ supports mutiple inheritance directly; Java supports multiple inheritance only through interfaces C++ supports operator overloading; Java does not C++ supports global variables; Java does not (sort of) C++ supports #define; Java does not C++ supports typedef; Java does not C++ supports enum; Java [...]...


  • Java Class

    A Java class is a group of Java methods and variables. Each Java source code file can contain one public class. The name of this public class must match the name of the Java source code file. If the public class is called “ballistics,” then the filename would be “ballistics.java.” Example Java Class class Ammunition [...]...


  • Java Method

    A Java method is a set of Java statements that can be included inside a Java class. Java methods are similar to functions or procedures in other programming languages. There are many different types of Java methods: Final Method Public Method Private Method Package Method Instance Method Protected Method Static Method Main Method The Main [...]...


  • Java Decompiler

    A Java decompiler is a special type of decompiler which takes a class file as input and produces Java source code as output. Java Decompilers Jode JODE is a Java package containing a decompiler and an optimizer for java. This package is freely available under the GNU GPL. The bytecode package and the core decompiler [...]...


  • Java Loops – For, While, and Do

    Java supports three types of loops: For, While, and Do. The Java For Loop The Java for loop is a looping construct which continually executes a block of statements over range of values. Java for Loop Syntax The syntax of a for loop in Java is: for (initialization; termination; increment) { statement } Example Java [...]...