Why should the interface stipulate that member variables must be public static final?

Source: Internet
Author: User

The variables inside the interface are public static and final by default. So you can omit the modifier directly:

String param= "SSM";//variables need to be initialized

Why should the interface stipulate that member variables must be public static final?

For:

First, the interface is a highly abstract "template", and the interface property is the ' template ' members, it should be all implementation of the "template" implementation of the common characteristics of the class, so it is public static, is all the implementation class common. If it can be non-static, Because a class can inherit multiple interfaces, there are variables with the same name, how to distinguish them?

Second, if the interface can define non-final variables, and the method is abstract, it is paradoxical that there are variable member variables but the corresponding methods can not manipulate these variables, although the values of these static member variables may be directly modified, but all the implementation class corresponding values are modified, What is the difference between this and an abstract class? Interface is a higher level of abstraction, is a specification, functional definition of the declaration, all the mutable things should be attributed to the implementation class, so that the interface can be standardized, standardized role. So the attributes in the interface must be final.

Finally, the interface is just a higher level of abstraction of the properties and behaviors of things. For the modification to close, open for extensions (different implementations implements), the interface is an embodiment of the closed principle (open-closed Principle).

Why should the interface stipulate that member variables must be public static final? (EXT)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.