Java类常量

时间:2012-11-13 09:53:30

标签: java

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

在我的类Response中我已经定义了一些我想在整个应用程序中用作常量的responseCodes。

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

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

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

我的带有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;
    }
}

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

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;
    }
}

我也试过

import static xxx.xxx.Response.*

import static xxx.xxx.Response.BR_PARTIALLY_OK;

并使用

return BR_PARTIALLY_OK

导入

时已经出现“无法找到符号”错误

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

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

的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>

2 个答案:

答案 0 :(得分:1)

我发现错误信息有点失效。

“变量BR_PARTIALLY_OK”

变量?这会让我假设代码不包含'Response.BR_PARTIALLY_OK'但只包含“BR_PARTIALLY_OK”。

答案 1 :(得分:1)

伙计们,感谢大家花在这上面的时间。问题确实与构建有关。这不是重建整个项目而是重建了其中一个工件(偶然),这导致了问题,因为const定义了在添加常量后从未重建的其他工件中的位置

构建父作用域会自动解决此问题。