Java 类常量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13358579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:25:22  来源:igfitidea点击:

Java class constants

java

提问by Michel Feldheim

I'd like to define a few public class constant-like variables for a class I also create an instance of.

我想为一个类定义一些公共类常量类变量,我也创建了一个实例。

In my class Response I've defined a few responseCodes which I'd like to use as constants all over the application.

在我的 Response 类中,我定义了一些 responseCodes,我想将它们用作整个应用程序中的常量。

Please don't worry about the sense of this code, this is just a copy and paste of snippets to show how I am trying to use the class constants.

请不要担心这段代码的含义,这只是片段的复制和粘贴,以展示我如何尝试使用类常量。

My IntelliJ doesn't complain about this syntax at all, but when I am trying to build the code I get

我的 IntelliJ 根本没有抱怨这种语法,但是当我尝试构建代码时,我得到了

Filter.java:[84,36] cannot find symbol
symbol  : variable BR_PARTIALLY_OK

My sample class with consts

我的示例类与常量

package xx.xx.xxxxx.connector;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Response {
    public static final int BR_OK                       = 200;
    public static final int BR_PARTIALLY_OK             = 250;
    public static final int BR_NOT_AUTHORIZED           = 401;
    public static final int BR_REQUEST_TIMEOUT          = 408;
    public static final int BR_NOT_IMPLEMENTED          = 501;
    public static final int BR_SERVICE_UNAVAILABLE      = 503;

    private Map<String, List<String>> headers = new HashMap<String, List<String>>();
    private String body = null;

    public void addHeader(String key, List<String> value) {
        this.headers.put(key, value);
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public Map<String, List<String>> getHeaders() {
        return headers;
    }
}

Desired example use in another class

在另一个类中使用所需的示例

package xx.xx.xxxxx.filter; // other package, other maven artifact but depends response package
import xx.xx.xxxxx.connector.Response;

public class Filter {
    public int doFilter(Service service) {
        Response myResponse = service.post(..);
        return Response.BR_PARTIALLY_OK;
    }
}

I've also tried

我也试过

import static xxx.xxx.Response.*

or

或者

import static xxx.xxx.Response.BR_PARTIALLY_OK;

and use

并使用

return BR_PARTIALLY_OK

same "cannot find symbol" error already on the import

导入时已出现相同的“找不到符号”错误

I've seen the interface and const class examples but I'd like to know why this example doesn't work

我看过接口和 const 类的例子,但我想知道为什么这个例子不起作用

EDIT: Maybe the problem is that the class is defined in a dependent artifact?

编辑:也许问题在于该类是在依赖工件中定义的?

pom.xml

pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>xxx.xxxxx.xxx</groupId>
        <artifactId>connector</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>used the Response.BR_PARTIALLY_OK constant here</groupId>
    <artifactId>filter</artifactId>
    <version>${connector.filter.version}</version>

    <dependencies>
        <dependency>
            <groupId>defined the Response class in this artifact</groupId>
            <artifactId>component</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>           

        <dependency>
            <groupId>info.magnolia</groupId>
            <artifactId>magnolia-core</artifactId>
            <version>4.5.4</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>net.sourceforge.openutils</groupId>
            <artifactId>openutils-log4j</artifactId>
            <version>2.0.5</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.4</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- Testing Dependencies -->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>magnolia.public</id>
            <url>http://nexus.magnolia-cms.com/content/groups/public</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

采纳答案by Michel Feldheim

Guys thank you all for the time you spent on this. The problem indeed was build-related. Instead of rebuilding the whole project I just rebuilt one of the artifacts (by accident) this caused the problem because the const definitions where in the other artifact which was never rebuilt after adding the constants

伙计们,感谢你们在这件事上花费的时间。问题确实与构建相关。而不是重建整个项目,我只是重建了一个工件(偶然)这导致了问题,因为常量定义在另一个工件中的位置在添加常量后从未重建

building the parent scope solved this automatically.

构建父作用域自动解决了这个问题。

回答by Gimby

I find the error message a bit off.

我发现错误信息有点不对劲。

"variable BR_PARTIALLY_OK"

“变量 BR_PARTIALLY_OK”

Variable? That would make me assume that the code does not contain 'Response.BR_PARTIALLY_OK" but only "BR_PARTIALLY_OK".

多变的?那会让我假设代码不包含“Response.BR_PARTIALLY_OK”而只包含“BR_PARTIALLY_OK”。