Why doesn\'t Java allow private members in interface? Is there any particular reason?
Private members don't make sense in interface. Interface is a way to access a class with defined methods where you don't need to see the inners of that class.
Private members disagree to that.
An interface is used for describing an API which is provided by any class implementing the interface. Since an interface from its definition has no state there is no use of declaring field members in it.
In Java 9, private methods in interfaces are possible.
Java 9 specifications
The javac compiler team is pleased announce the availability of compiler support for private methods in interfaces beginning with 9 b54 build of JDK.
From the Java Language Spec, (Access Control):
"The Java programming language provides mechanisms for access control, to prevent the users of a package or class from depending on unnecessary details of the implementation of that package or class."
Access control is all about hiding implementation details. An interface has no implementation to hide.
Members of a class that are declared private are not inherited by subclasses of that class. Only members of a class that are declared protected or public are inherited by subclasses declared in a package other than the one in which the class is declared.
Source
So you don't have any working methods in an interface which can work with that private non-inheritable field, Then why should it exist?
Yep, can't do that. For all those commenting on why it shouldn't:
Imagine I have Class A, which utilizes interface I. Class B, extends Class A, therefore also inheriting all interface methods in A.
Now, imagine I want a private method in Class A, but want it contractually defined for other classes as well (Maybe a class C, which doesn't necessarily extend Class B or A).
Perhaps for an "initialization" method, that I want for all classes using an I interface. But obviously I don't want an initialization method to be public.... since it should only be used once, or as the class deems necessary, not just because you want to use it all willy-nilly.
The only solution is a workaround, or by simply forcing the init method into the classes themselves without an interface.
I understand the reason not too, for sure, but still, it can come in handy sometimes. Clearly Oracle agrees as they're allowing private interface methods in JDK 9.
What I did, for mine anyway, was place a simple boolean variable, that way the interface method (which should be private) can be flagged as true (initialized = true) after being set once. Then when called again the method simply does nothing. This way the interface method can be implemented as public, but since the constructor (of my class) calls the method first, this sets the variable to true, and so it can't be called again.
Otherwise you'd have to try a different workaround if you only want the inner workings of the class to use it.... perhaps a method itself sets a flag on and off as it uses it. When the flag is false, the method does nothing (this would be when someone calls it from outside the class). However, when the classes own methods call it, they quickly set the flag to true, then call the method, then set the flag to false??
In the end kind of mute. Probably just better for now to simply place the private class into the class itself and cut-out the interface altogether.