2

Recently I saw in somebody's code that he implements his final class variable fields inside an interface ex:

public interface commentSchema{
  static final String DB_TABLE_NAME = "comment";
  ...
}

& he had implemented a class which does need these variables something like this:

public class DbComment implements commentSchema {
  // use the DB_TABLE_NAME value here ...
  ...
}

as you know If someone make an instance of DbComment class because of inheritance aspect, he's gonna be able to access the DB_TABLE_NAME which is not proper because we want to use those values only inside DbComment methods.

now I have several questions:

1) is this implementation proper & ok ?

2) if It's not, how we have to declare these variables outside of DbComment class & make this class be the only class who does see these values. (we don't want to use abstract class coz a class can only extends one other class in java)

3) why we do need to use static for values & methods which exist inside an interface ? (when we implements certain interface for a specific class why do we need to make it static to be seen everywhere?)

4) is there any specification that exactly determine kinds of different declarations for java methods, classes, enums, etc ?

Mehdi
  • 3,795
  • 3
  • 36
  • 65

2 Answers2

2

1) is this implementation proper & ok ?

Yes, will work just fine.

2) if It's not how I'm gonna declare these variable outside of DbComment class & make this class be the only class who does see DB_TABLE_NAME value. (I don't want to use abstract class coz a class can only extends one other class in java)

No need as the implementation used works as expected.

3) why we do need to use static for values & methods which exist inside an interface ? (when we implements certain interface for a specific class why do we need to make it static to be seen everywhere?)

You can't make methods in interfaces final or static. The only qualifiers allowed for methods are public and abstract which have, by the way, no effect at all.

For fields, static exists, but also has no effect. All fields declared in a interface will be accessible statically by the implementors and are considered constants.

4) is there any specification that exactly determine kinds of different declarations for java methods, classes, enums, etc ?

The official spec has a chapter on Names and Declaration.

acdcjunior
  • 132,397
  • 37
  • 331
  • 304
1

By default, any field declared inside an interface is marked as public static final even if the programmer doesn't do it. This means, any field in the interface will be constant and impossible to modify.

If you don't want that functionality (whatever reasons you could have) then it would be better to have an abstract class instead and mark the field as protected static final.

public abstract class CommentSchema{
    protected static final String DB_TABLE_NAME = "comment";
}

Still, if you want to base your design to interfaces, then you can have the interface without this field, then an abstract class that implements this interface and adds the field. By doing this, every class that extends the abstract class will implement the interface and have access to the field:

public interface CommentSchema {
    foo();
}

public abstract class CommentSchemaAbstractImpl implements CommentSchema {
    protected static final String DB_TABLE_NAME = "comment";
}

public class CommentSchemaRealImpl extends CommentSchemaAbstractImpl {
    @Override
    public void foo() {
        //do something...
        baz(DB_TABLE_NAME);
    }
    private void baz(String s) {
        //fancy code here...
    }
}

At last, you can forget about all this and create an enum to handle your constants.

public enum TableName {
    COMMENT("comment");
    private String tableName;
    private TableName(String tableName) {
        this.tableName = tableName;
    }
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332