我想定義一個類的一些公共類常量變量我也創建一個實例。Java類常量
在我的類中響應我已經定義了一些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
相同的「無法找到符號」錯誤'd想知道爲什麼這個例子不起作用
編輯: 也許問題是這個類是在一個相關的工件中定義的?
的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>
待辦事項你在IntelliJ裏面或者在單獨的版本中得到「無法找到符號」? –
你可以通過Classname.staticvariable使用靜態變量,你不需要導入。變量可用於整個應用程序。 – Raghunandan
我在Intellij中使用了maven編譯 –