Michel Feldheim
Michel Feldheim

Reputation: 18250

Java class constants

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.

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

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

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

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>

Upvotes: 2

Views: 19827

Answers (2)

Michel Feldheim
Michel Feldheim

Reputation: 18250

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.

Upvotes: 1

Gimby
Gimby

Reputation: 5284

I find the error message a bit off.

"variable BR_PARTIALLY_OK"

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

Upvotes: 1

Related Questions