Why am I not getting compile error when initializing an instance variable in an interface in java
So here is the interface code. I searched the internet if you can have an instance variable in the interface and I found out that I couldn't, but when I tried to initialize the instance variable in the interface I didn't get a compile time error.
public interface hgf {
public void eat();
int x = 0;
}
+3
Adam
source
to share
1 answer
The reason is the variable x, which you think is an instance variable, is actually a constant. Interfaces define variables with static endings by default, even if you don't see them. Hope this helps :)
+3
Aaron
source
to share