Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, constants are values that cannot be changed once they are assigned. Constants are useful when you need a value that remains constant throughout the lifetime of the application. In this tutorial, we will cover how to create and use constants in Java.
final
KeywordIn Java, constants are created using the final
keyword. When the final
keyword is used with a variable, it makes the variable's value unmodifiable. Constants are typically named using uppercase letters with underscores for spaces, following the naming convention for constants.
Example:
public class ConstantsExample { public static void main(String[] args) { // Declare a constant final double PI = 3.14159265359; // Calculate the circumference of a circle double radius = 5.0; double circumference = 2 * PI * radius; System.out.println("Circumference of the circle: " + circumference); } }
static final
KeywordsWhen you need to create a constant that is shared across all instances of a class, you can use the static
keyword in combination with the final
keyword. The static
keyword ensures that the constant belongs to the class itself rather than any particular instance of the class.
Example:
public class ConstantsExample { // Declare a static final constant public static final double PI = 3.14159265359; public static void main(String[] args) { // Calculate the circumference of a circle double radius = 5.0; double circumference = 2 * PI * radius; System.out.println("Circumference of the circle: " + circumference); } }
It's a good practice to store constants that are used across multiple classes in a separate class. This makes it easy to manage and maintain the constants in a centralized location.
Example:
// Constants.java public class Constants { public static final double PI = 3.14159265359; public static final double E = 2.71828182846; } // Main.java public class Main { public static void main(String[] args) { // Calculate the circumference of a circle double radius = 5.0; double circumference = 2 * Constants.PI * radius; System.out.println("Circumference of the circle: " + circumference); } }
In summary, constants in Java are created using the final
keyword, and the static
keyword is used to make them class-level constants. It's a good practice to store constants that are shared across multiple classes in a separate class. Always follow the naming convention of using uppercase letters with underscores for spaces when naming constants.
Defining constants in Java
Constants in Java are values that do not change during the program's execution. They are declared using the final
keyword to indicate that the value cannot be modified.
public class ConstantsExample { public static final int MAX_VALUE = 100; public static final String APP_NAME = "MyApp"; }
Final keyword in Java for creating constants
The final
keyword in Java is used to declare constants, indicating that the value of the variable cannot be changed once assigned.
public class ConstantsExample { public static final double PI = 3.14159; public static final int MAX_SIZE = 100; }
Java constant naming conventions
Constants in Java are typically named using uppercase letters with underscores to separate words. This convention improves readability and distinguishes constants from variables.
public class ConstantsExample { public static final int MAX_VALUE = 100; public static final String APP_NAME = "MyApp"; }
Using static final for constants in Java
Constants are often declared as static final
to indicate that they belong to the class rather than an instance and that their values are constant.
public class ConstantsExample { public static final int MAX_VALUE = 100; public static final String APP_NAME = "MyApp"; }
Enumerations as constants in Java
Enumerations (enums) provide a way to represent a fixed set of constants. Each constant in an enum is a public, static, final field.
public enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
Global constants in Java classes
Global constants are constants that are accessible throughout the entire program. They are often placed in utility classes or interfaces for easy access.
public class GlobalConstants { public static final String DATABASE_URL = "jdbc:mysql://localhost:3306/mydb"; public static final String API_KEY = "my_api_key"; }
Java constant interface pattern
The constant interface pattern involves creating an interface with only public static final
constants. However, this approach is discouraged due to potential issues with multiple interface implementations.
public interface MyConstants { public static final int MAX_VALUE = 100; public static final String APP_NAME = "MyApp"; }
Constants in Java interfaces
Interfaces in Java can also define constants. All fields in interfaces are implicitly public
, static
, and final
.
public interface MyConstants { int MAX_VALUE = 100; String APP_NAME = "MyApp"; }
Immutable objects and constants in Java
Constants are often made immutable (not subject to change) to ensure their values remain constant throughout the program.
public class ImmutableConstants { public static final String COMPANY_NAME = "MyCompany".intern(); public static final int MAX_RETRY_ATTEMPTS = 3; }