I was trying to test working of private
interfaces
and wrote the code below. I can understand that a situation might arise to declare private
interfaces
if we don't want any other class to implement them but what about variables? Interface variables are implicitly public static final
and hence i was able to access them even if interface was declared private. This can be seen in code below.
public class PrivateInterfaceTest {
/**
* @param args
*/
public static void main(String[] args) {
TestingInterfaceClass test = new TestingInterfaceClass();
TestingInterfaceClass.inner innerTest = test.new inner();
System.out.println(innerTest.i);
}
}
class TestingInterfaceClass {
private interface InnerInterface {
int i = 0;
}
class inner implements InnerInterface {
}
}
Does it mean that we can never really have private interface
in true sense? And does it really make sense to if have private interface
if we can access variables outside private interface
?
EDIT: Just want to add that same situation will not arise if we have private inner class. A private variable in inner class will never get exposed.