1

So I have this interface QueryAndRetrieval and I would like to force each of the classes that implements it to have a few variables as well as a few very simple methods, however if I try to define a vaiable as the one on line 5

public interface QueryAndRetrieval {

    public String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
    public String JDBC_URL = "jdbc:derby:CompanyDB;create=true";
    public static Connection conn;

    public boolean containsEntry(String TableName, Object key);

    public static Connection connect() throws SQLException {
        return DriverManager.getConnection(JDBC_URL);
    }
}

Eclipse informs me that

The blank final field conn may not have been initialized

but there is no final field within the interface whatsoever. Could anyone tell me why this is happening and how to resolve it? Please assume that all the necessary and correct imports are present within the interface.

Ohunter
  • 343
  • 1
  • 2
  • 13
  • 3
    `QueryAndRetrieval` is an interface, all fields in any interface are implicitly `public static final`... – Oleksandr Pyrohov Jun 06 '18 at 20:22
  • 1
    "The point of an interface is to specify the public API. An interface has no state." ... See [this answer](https://stackoverflow.com/a/7311394/17300) to the question [Attributes / member variables in interfaces?](https://stackoverflow.com/q/7311274/17300) – Stephen P Jun 06 '18 at 21:41
  • 1
    Possible duplicate of [Attributes / member variables in interfaces?](https://stackoverflow.com/questions/7311274/attributes-member-variables-in-interfaces) – Didier L Jun 06 '18 at 21:55

2 Answers2

2

In Java an interface specifies an API that all implementors must respond to. How they do that is up to the implementation, so forcing a field on the implementor makes no sense.

If you want to provide some ease-of-implementation support, the way to do that is with an interface and an abstract base class — for example

public interface QueryAndRetrieval {

    public String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
    public String JDBC_URL = "jdbc:derby:CompanyDB;create=true";

    public boolean containsEntry(String TableName, Object key);

    public abstract Connection connect() throws SQLException;
}

public abstract class QueryAndRetrievalBase {
    public Connection conn;

    public Connection connect() throws SQLException {
        return DriverManager.getConnection(QueryAndRetrieval.JDBC_URL);
    }
}

public class MyQueryImpl extends QueryAndRetrievalBase { ... }

Note that, as of Java 8, an interface can also provide default methods, but you still can't declare member variables in the interface, so those default methods still can't use "abstract" members.

Stephen P
  • 14,422
  • 2
  • 43
  • 67
1

In interface all properties are public static final by default. So if you do not use conn property, just remove it.

If you are using it you might need to have static initialization block in your code to set the value to conn

Ivan
  • 8,508
  • 2
  • 19
  • 30