静态字段应以静态方式访问 [英] The static field should be accessed in a static way

查看:397
本文介绍了静态字段应以静态方式访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下不同的异常类别枚举

I have different Exception category Enum as below

public enum GSBBCacheCategory  {
    SEARCH(9001),
    UPDATE_PERSECURITY(9002),
    CROSS_REFERENCING_PERSECURITY(9003),
    METADATA_SEARCH(9004),
    REMOVEALL(9005),
    UPDATE_BACKOFFICE(9002);

    private int exceptionCode;

    GSBBCacheCategory(int exceptionCode)
    {
        this.exceptionCode = exceptionCode;
    }

    public int getExceptionCode()
    {
        return exceptionCode;
    }
} 

public enum GSBBEncryptionCategory  {
.
.
.
}

我想在客户端代码中提供一个访问这些枚举的位置。目前,我已达到以下目标

I want to provide one place to access these Enum in client code. Presently I achieved this as below

public class GSBBExceptionCodes
{
 public static GSBBDecryptionCategory decryptionCategory;
 public static GSBBCacheCategory cacheCategory;
}

现在要访问异常代码,我已经在

Now to access exception code I have do something like below

public static void main(String[] args) {
     System.out.println(GSBBExceptionCodes.decryptionCategory.ERRORCODE_DECRYPTION_FAILURE);
     System.out.println(GSBBExceptionCodes.cacheCategory.UPDATE_PERSECURITY);
}

其中说 静态字段GSBBDecryptionCategory.ERRORCODE_DECRYPTION_FAILURE应该在

是否可以在没有任何警告的情况下实现以上目标?

Is it possible to achieve above without any warning?

推荐答案

有两种方法可以引用静态成员(字段或方法)。一个是 WhateverClass.theField ,另一个是 someInstance.theField 其中 someInstance 的编译时类型为 WhateverClass 。前者要清晰得多,因此您的IDE会告诉您使用它而不是后者。

There are two ways to reference a static member (either a field or a method). One is WhateverClass.theField, and the other is someInstance.theField where someInstance has a compile-time type of WhateverClass. The former is much clearer, and so your IDE is helpfully telling you to use it instead of the latter.

更好的原因是通过实例引用静态成员使得该方法看起来与该实例有关,而与实例无关。这是一个真实的示例:

The reason it's better is that referencing a static member by an instance makes it look like the method has something to do with that instance, which it doesn't. Here's a real-life example:

Thread myThread = getMyThread();
myThread.start();
myThread.sleep(5000);

乍一看,好像是在问 myThread current 线程进入睡眠状态,因为最后一行与调用 Thread.sleep(5000)完全相同。第二个例子显然是一个静态方法。

At first blush, it looks like you're asking myThread to sleep for 5 seconds (5000 milliseconds), but that's not what you're doing at all. You're asking the current thread to sleep, because that last line is exactly the same as invoking Thread.sleep(5000). That second example is much more clearly a static method.

或者,这是另一个例子。假设您的静态字段是可变的。

Or, here's another example. Let's say your static fields were mutable.

public class Foo {
    public static int value = 1;
}

(此公共静态可变字段出于其他原因不是一个好主意,但可以简化这个例子)。现在,假设您这样做:

(This public static mutable field is a bad idea for other reasons, but simplifies the example). Now let's say you do:

Foo one = new Foo();
Foo two = new Foo();
one.value = 2;
two.value = 3;
System.out.println(one.value);
System.out.println(two.value);

Kinda看起来应该先打印 2,然后打印 3,对吗?但是不会-它会显示 3, 3,因为对 .value 的两个分配实际上都属于相同的静态字段。 一个两个实例与任何事情都只是虚假的幻觉。

Kinda looks like that should print "2" and then "3", right? But no -- it'll print "3", "3" because both assignments to .value are in fact to the same, static field. It's just an optical illusion that the one or two instances have anything to do with anything.

Imho,从实例引用静态成员的功能是不适当的。但是它在那里,所以您应该避免它。编译器试图建议您这样做。

Imho, the ability to reference static members from instances is a misfeature. But it's there, so you should avoid it. Which is what the compiler is trying to suggest you do.

这篇关于静态字段应以静态方式访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆