Skip to content

常量类

java
package com.example.constants;

public final class AppConstants {

    // 私有化构造函数,防止实例化
    private AppConstants() {
        throw new UnsupportedOperationException("This is a constants class and cannot be instantiated");
    }

    // 通用状态
    public static final String STATUS_ACTIVE = "active";
    public static final String STATUS_INACTIVE = "inactive";

    // 错误码
    public static final int ERROR_CODE_404 = 404;
    public static final int ERROR_CODE_500 = 500;

    // 默认值
    public static final int DEFAULT_PAGE_SIZE = 20;
    public static final String DEFAULT_ENCODING = "UTF-8";
}

特点:

  • final 修饰类,防止被继承。
  • final修饰变量,防止被篡改
  • 私有构造函数,防止实例化
  • public static 修饰常量,方便全局调用。
  • 常量命名一般使用全大写加下划线分隔单词