In application development, certain values like mathematical PI or the number of days in a year remain constant throughout the entire execution. This project demonstrates the creation of a Utility Class for global constants. By combining public, static, and final modifiers, we make these values globally accessible, shared across the JVM, and protected from any modification.
- Static Access: Constants are accessible via the class name without instantiation.
- Immutability: The
finalkeyword prevents any changes to the values. - Naming Convention: Followed the
UPPER_SNAKE_CASEstandard for constants. - Global Visibility: Used the
publicmodifier to allow access from any package.
- Java 8+ (Access Modifiers, Static Constants, Naming Conventions)
- GlobalConstants: A non-instantiable utility class holding shared fixed data.
- GlobalConstantsApp: The consumer class that demonstrates direct field access.
The number PI: 3.14159
Standard number of days in a year: 365
Project Structure:
JavaBasics_Task_259/
├── src/
│ └── com/yurii/pavlenko/
│ ├── GlobalConstants.java
│ └── GlobalConstantsApp.java
└── README.md
Code
package com.yurii.pavlenko;
public class GlobalConstantsApp {
public static void main(String[] args) {
System.out.println("The number PI: " + GlobalConstants.MATH_PI);
System.out.println("Standard number of days in a year: " + GlobalConstants.CALENDAR_DAYS_IN_YEAR);
}
}package com.yurii.pavlenko;
public class GlobalConstants {
public static final double MATH_PI = 3.14159;
public static final int CALENDAR_DAYS_IN_YEAR = 365;
}This project is licensed under the MIT License.
Copyright (c) 2026 Yurii Pavlenko
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files...
License: MIT