Thursday, 26 December 2019

Why the interface variables are public, static, final by default?

As per Java specification, every field declaration in the body of an interface is implicitly public, static, and final.

Why the interface variables are static?
Interface variables are static because Java interfaces cannot be instantiated by their own, so the variables should be defined in static context.

Why the interface variables are final?
Interface is a specification. It can be implemented by multiple classes. If the variables are not final, then there is a chance that implementing classes can change the value of interface variable. To avoid this, variables in interface are implicitly final.

Why the interface variables are public?
To be able to access by all the implementing classes, interface variables are public.

You may like

No comments:

Post a Comment