Java中定義常量方法及建議(Class/Interface)

Class定義常量方法(推薦方法)

//final修飾符
public final class Constants {
    //私有構(gòu)造方法
    private Constants() {}

    public static final int ConstantA = 100;
    public static final int ConstantB = 100;
    ......
}

采用“類.常量名”方法進(jìn)行調(diào)用。需要私有化構(gòu)造方法,避免創(chuàng)建該類的實(shí)例。同時(shí)不需讓其他類繼承該類。

如果多處需要訪問工具類中定義的常量,可以通過靜態(tài)導(dǎo)入(static import)機(jī)制,避免用類名來修飾常量名。

Interface定義常量方法

public interface Constants {
    int ConstantA = 100;
    int ConstantB = 100;
    ......
}

在interface中聲明的字段,虛擬機(jī)在編譯時(shí)自動加上public static final修飾符。使用方法一般是“接口.常量名”。也可以通過實(shí)現(xiàn)該接口,直接訪問常量名,即常量接口模式。

常量接口:即接口中不包含任何方法,只包含靜態(tài)的final域,每個(gè)域都導(dǎo)出一個(gè)常量。使用這些常量的類實(shí)現(xiàn)這個(gè)接口,以避免用類名來修飾常量名。

常量接口模式是對接口的不良使用。具體參考如下:

The constant interface pattern is a poor use of interfaces. That a class uses some constants internally is an implementation detail. Implementing a constant interface causes this implementation detail to leak into the class's exported API. It is of no consequence to the users of a class that the class implements a constant interface. In fact, it may even confuse them. Worse, it represents a commitment: if in a future release the class is modified so that it no longer needs to use the constants, it still must implement the interface to ensure binary compatibility. If a nonfinal class implements a constant interface, all of its subclasses will have their namespaces polluted by the constants in the interface.

There are several constant interfaces in the java platform libraries, such as java.io.ObjectStreamConstants. These interfaces should be regarded as anomalies and should not be emulated.

區(qū)別

上述兩種方法對比,interface中定義常量方法生成的class文件比第一種方法的class文件更小, 且代碼更簡潔, 效率更高.

但是在java中會產(chǎn)生問題,主要是java的動態(tài)性,java中一些字段的引用可以在運(yùn)行期動態(tài)進(jìn)行。某些場景下,部分內(nèi)容改變可只進(jìn)行部分編譯。具體例子參考文檔Java Interface 是常量存放的最佳地點(diǎn)嗎?

該文推薦使用Class定義常量,但采用private修飾符,通過get方法獲取常量。這種方案可以保證java的動態(tài)性。

public class A{
    private static final String name = "bright";
    public static String getName(){
        return name;
    }
}

參考文檔:
https://stackoverflow.com/questions/2659593/what-is-the-use-of-interface-constants
Java Interface 是常量存放的最佳地點(diǎn)嗎?
關(guān)于Java常量定義的一點(diǎn)思考

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 對象的創(chuàng)建與銷毀 Item 1: 使用static工廠方法,而不是構(gòu)造函數(shù)創(chuàng)建對象:僅僅是創(chuàng)建對象的方法,并非Fa...
    孫小磊閱讀 2,185評論 0 3
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,734評論 18 399
  • 一、JVM內(nèi)幕:Java虛擬機(jī)詳解(java se 7規(guī)范) 直接上圖,再逐步解釋。 上圖顯示的組件分兩個(gè)章節(jié)解釋...
    屈小勇閱讀 2,017評論 6 22
  • 在和所謂的環(huán)境、社會、傳統(tǒng)進(jìn)行抗?fàn)帟r(shí),內(nèi)心其實(shí)是充滿焦慮的:因?yàn)橐膊恢雷约阂ズ畏?,只是知道似乎不喜歡什么,而不...
    睿子愛晴天閱讀 231評論 0 0
  • 當(dāng)一艘船沉入海底,當(dāng)一個(gè)人成了謎。你不知道,他們?yōu)楹坞x去,那聲再見竟是他最后一句。 ——————————題記 這是...
    一個(gè)瘋子的世界jasmine閱讀 215評論 0 1

友情鏈接更多精彩內(nèi)容