default methods in interface but only static final fields
Asked Answered
R

4

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???

Roentgenology answered 30/6, 2015 at 17:4 Comment(1)
No. Nonstatic fields are the primary remaining difference between interfaces and abstract classes.Entertain
B
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;
        }
    }
}
Breezeway answered 30/6, 2015 at 17:11 Comment(3)
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???Roentgenology
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.Breezeway
@AbishekManoharan: Yup, pretty much.Entertain
T
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.

Takeover answered 30/6, 2015 at 17:8 Comment(6)
There were no fields before Java 8 :)Hagiology
There were fields in interfaces from Java inception.Breezeway
@DmitryGinzburg What do you mean? Java has always allowed to declare fields in interfaces (which happen to be automatically static and final).Takeover
@Takeover If they could avoid the problem with methods, why not extend it to fields?Carlsen
@Takeover ooops, some temporary madness.Hagiology
As Chetan has metioned, the diamond problem was avoided for methods I guess.. And the same approach could be used for fields also... And default methods wont always be useful if it can only operate on local variables...Roentgenology
C
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());
    }
}
Corposant answered 30/11, 2018 at 5:3 Comment(0)
F
-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

Fantasize answered 14/11, 2017 at 9:41 Comment(2)
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.3Favour
I meant fields that are not public static final. I should have made that more clear apologies. I will update my answer.Fantasize

© 2022 - 2024 — McMap. All rights reserved.