Note that there are some explanatory texts on larger screens.

plurals
  1. POJava class constants
    text
    copied!<p>I'd like to define a few public class constant-like variables for a class I also create an instance of.</p> <p>In my class Response I've defined a few responseCodes which I'd like to use as constants all over the application.</p> <p>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.</p> <p>My IntelliJ doesn't complain about this syntax at all, but when I am trying to build the code I get </p> <pre><code>Filter.java:[84,36] cannot find symbol symbol : variable BR_PARTIALLY_OK </code></pre> <p>My sample class with consts</p> <pre><code>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&lt;String, List&lt;String&gt;&gt; headers = new HashMap&lt;String, List&lt;String&gt;&gt;(); private String body = null; public void addHeader(String key, List&lt;String&gt; value) { this.headers.put(key, value); } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public Map&lt;String, List&lt;String&gt;&gt; getHeaders() { return headers; } } </code></pre> <p>Desired example use in another class</p> <pre><code>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; } } </code></pre> <p>I've also tried</p> <pre><code>import static xxx.xxx.Response.* </code></pre> <p>or</p> <pre><code>import static xxx.xxx.Response.BR_PARTIALLY_OK; </code></pre> <p>and use</p> <pre><code>return BR_PARTIALLY_OK </code></pre> <p>same "cannot find symbol" error already on the import</p> <p>I've seen the interface and const class examples but I'd like to know why this example doesn't work</p> <p>EDIT: Maybe the problem is that the class is defined in a dependent artifact?</p> <p>pom.xml</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;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"&gt; &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt; &lt;parent&gt; &lt;groupId&gt;xxx.xxxxx.xxx&lt;/groupId&gt; &lt;artifactId&gt;connector&lt;/artifactId&gt; &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt; &lt;/parent&gt; &lt;groupId&gt;used the Response.BR_PARTIALLY_OK constant here&lt;/groupId&gt; &lt;artifactId&gt;filter&lt;/artifactId&gt; &lt;version&gt;${connector.filter.version}&lt;/version&gt; &lt;dependencies&gt; &lt;dependency&gt; &lt;groupId&gt;defined the Response class in this artifact&lt;/groupId&gt; &lt;artifactId&gt;component&lt;/artifactId&gt; &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;info.magnolia&lt;/groupId&gt; &lt;artifactId&gt;magnolia-core&lt;/artifactId&gt; &lt;version&gt;4.5.4&lt;/version&gt; &lt;scope&gt;provided&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;net.sourceforge.openutils&lt;/groupId&gt; &lt;artifactId&gt;openutils-log4j&lt;/artifactId&gt; &lt;version&gt;2.0.5&lt;/version&gt; &lt;scope&gt;provided&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;javax.servlet&lt;/groupId&gt; &lt;artifactId&gt;servlet-api&lt;/artifactId&gt; &lt;version&gt;2.4&lt;/version&gt; &lt;scope&gt;provided&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;javax.servlet&lt;/groupId&gt; &lt;artifactId&gt;jsp-api&lt;/artifactId&gt; &lt;version&gt;2.0&lt;/version&gt; &lt;scope&gt;provided&lt;/scope&gt; &lt;/dependency&gt; &lt;!-- Testing Dependencies --&gt; &lt;dependency&gt; &lt;groupId&gt;org.mockito&lt;/groupId&gt; &lt;artifactId&gt;mockito-all&lt;/artifactId&gt; &lt;version&gt;1.9.5&lt;/version&gt; &lt;scope&gt;test&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;junit&lt;/groupId&gt; &lt;artifactId&gt;junit&lt;/artifactId&gt; &lt;version&gt;${junit.version}&lt;/version&gt; &lt;scope&gt;test&lt;/scope&gt; &lt;/dependency&gt; &lt;/dependencies&gt; &lt;repositories&gt; &lt;repository&gt; &lt;id&gt;magnolia.public&lt;/id&gt; &lt;url&gt;http://nexus.magnolia-cms.com/content/groups/public&lt;/url&gt; &lt;snapshots&gt; &lt;enabled&gt;true&lt;/enabled&gt; &lt;/snapshots&gt; &lt;/repository&gt; &lt;/repositories&gt; &lt;/project&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload