Why can final constants in Java be overridden?

后端 未结 6 1478
暗喜
暗喜 2020-12-28 14:25

Consider the following interface in Java:

public interface I {
    public final String KEY = \"a\";
}

And the following class:



        
6条回答
  •  不知归路
    2020-12-28 14:54

    You should not access your constant in this way, use the static reference instead:

    I.KEY //returns "a"
    B.KEY //returns "b"
    

提交回复
热议问题