Junior — Middle
Is it possible to declare variables with a private modifier inside an interface?
sobes.tech AI
Answer from AI
In Java, it is not possible to declare variables with the private modifier inside an interface. All variables in an interface are by default:
- public
- static
- final
That is, they are effectively constants accessible to anyone using the interface.
Example:
public interface MyInterface {
int CONSTANT = 10; // public static final by default
}
Attempting to declare a variable with the private modifier inside an interface will result in a compile-time error.
Starting from Java 9, it is possible to declare private methods inside interfaces, but not private variables.