Why interface variables are final




















Explain about WeakHashMap. Class Name of anonymous class. Is length property of array final? Labels: final , interfaces , Java , public , static. No comments:. Newer Post Older Post Home. Subscribe to: Post Comments Atom. From my point, variable in class is an attribute which will be inherited by children, and children can change it according to their actual need. On the contrary, interface only define behavior, not attribute. The only reason to put in variables in interface is to use them as consts which related to that interface.

Though, this is not a good practice according to following excerpt:. As well, the constants used by a class are typically an implementation detail, but placing them in an interface promotes them to the public API of the class. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Why are interface variables static and final by default?

Ask Question. Asked 11 years, 8 months ago. Active 1 year, 9 months ago. Viewed k times. Why are interface variables static and final by default in Java?

Improve this question. Jothi Jothi You shouldn't put any variables inside Interfaces. Because interfaces define contracts which can be implemented in various ways. The value of a variable is implementation. We certainly can when we know all the classes implementing the interface have some constant variables Field names for instance.

Is it a good idea to make a variable in a class an instance of the interface that the class implements? I have heard this before. Answer by Arun Raaj answered Apr 24 '18 at and comment by denis Aug 17 '17 at correctly identify Multiple Inheritance as the main issue Show 2 more comments. Active Oldest Votes.

From the Java interface design FAQ by Philip Shaw: Interface variables are static because Java interfaces cannot be instantiated in their own right; the value of the variable must be assigned in a static context in which no instance exists. Improve this answer. Dave Jarvis Note that abstract classes cannot be instantiated "in their own right" neither and those can have instance variables.

This explanation for the static modifier is completely spurious. A class's public instance variables are part of its interface and there's no reason why they shouldn't be abstracted in a Java interface , just like instance methods.

It doesn't matter that a Java interface can't be instantiated directly - you can still have instances of classes that implement the interface and it's sensible to require that they have a certain public instance variable.

As for the part about final , that doesn't offer an explanation at all - it just describes what final means. The quote above is better in context. The reason it gives is that "interface variables are intended to be Java constants". The quote was just elaborating on why such a constant would be static and final.

That's true, but the real question is: why aren't variables allowed as part of the actual interface ie. If they wanted special "interface constants", they could have used new syntax, or just decided that any variables actually defined in an interface are interface constants. Interfaces cannot have instance variables in order to avoid multiple inheritance of state problems. See docs. A class cannot extend more than one class due to the same reason.

How is default methods introduced and they do have instance, yet instance variable is not supported Show 3 more comments. Wouter Oet. I like They are implicitly static and final because instance variables don't work with polymorphism.

They can be used to allow implementing classes to have access to the same set of constants. Muhammad Khojaye. Arun Giridharan. Interface came into picture to solve the problem of Diamond shape problem. Interface where introduced to allow programming to an interface and not an implementation.



0コメント

  • 1000 / 1000