9

I understand that all fields in an Inteface is implicitly static and final. And this made sense before Java 8.

But with the introduction of default methods, interfaces also have all the capabilities of an abstract class. And hence non-static and non-final fields are also necessary.

But when I tried declaring a field normally, it became static and final by default.

Is there a way to declare a non-static and non-final field in Interface in Java 8.

Or am I totally misunderstanding something here???

1
  • No. Nonstatic fields are the primary remaining difference between interfaces and abstract classes. Commented Jun 30, 2015 at 17:36

4 Answers 4

14

All fields in interfaces in Java are public static final.

Even after addition of default methods, it still does not make any sense to introduce mutable fields into the interfaces.

Default methods were added because of interface evolution reasons. You can add a new default method to the interface, but it only makes sense if the implementation uses already defined methods in the interface:

public interface DefaultMethods {

    public int getValue();

    public default int getValueIncremented() {
        if (UtilityMethod.helper()) { // never executed, just to demonstrate possibilities
            "string".charAt(0); // does nothing, just to show you can call instance methods
            return 0;
        }

        return 1 + getValue();
    }

    public static class UtilityMethod {

        public static boolean helper() {
            return false;
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

So do you mean default methods purpose is to only include variations of existing methods and not introduce totally new methods which need to operate on object state???
Well that was the purpose. You still cannot add mutable fields into interfaces. Default methods can call static methods and instance methods on other objects, and all abstract and default methods defined in the interface. Look at the code I just updated.
@AbishekManoharan: Yup, pretty much.
4

No - in Java 8 all fields are static and final as in previous Java versions.

Having state (fields) in an interface would raise issues, in particular with relation to the diamond problem.

See also this entry that clarifies the difference between behaviour and state inheritance.

6 Comments

There were no fields before Java 8 :)
There were fields in interfaces from Java inception.
@DmitryGinzburg What do you mean? Java has always allowed to declare fields in interfaces (which happen to be automatically static and final).
@assylias If they could avoid the problem with methods, why not extend it to fields?
@assylias ooops, some temporary madness.
|
0

I strongly do not recommend to do that. Use abstract classes instead. But if you really need it you can do some workaround trick with some wrapper class. Here is an example:

public interface TestInterface {

    StringBuilder best = new StringBuilder();

    default void test() {
        best.append("ok");
        System.out.print(best.toString());
    }
}

Comments

-2

No - In java 8 you can't have not public static final fields in interfaces, unfortunately.

But I hope in the future this will be added to the language.

Scala allows interface with not static fields, and this opens up so many more possibilities for code reuse and composition than available in Java 8

2 Comments

You can have fields in Interfaces but only public, static and final fields are allowed. Refer docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.3
I meant fields that are not public static final. I should have made that more clear apologies. I will update my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.