Why can't I define a variable in an interface

Source: Internet
Author: User

The Java syntax stipulates that a variable in an interface is automatically implied by default and is public static final. Why you need to define this. The purpose of the interface an important concept in OOP is encapsulation, where objects can provide a series of methods that allow users to use objects to accomplish the tasks they need through the design of public interface. However, the internal implementation details are hidden from the user, and are intended to be used in the same way for classes that have common characteristics, and can be converted to classes. For example, consider playing for your vcr,dvd, tape recorder, CD, laser disk player, write a Home unified remote control, the remote control for 5 devices, have the same button, play, stop, fast-forward, repeat, pause you may notice, did not start, turn on the laser, open the magnet and so on button, These details are hidden, not for the necessary security reasons, but more correctly because these are the implementation parts of a specific device, allowing for easy switching from device to device and not for all devices. The primary purpose of the interface is to provide the details (remote control) that are available to the user in the device. This is why all the methods in the interface are implicitly public by default: These methods are intended to be accessible to the user. This is the purpose of the interface usage. Another important design principle of OOP, in almost all cases, a good object design does not provide customers with the ability to directly access its instance variables. Typically, those objects should define access and fetch methods for variables to implement the operation of variables. In this way, the designer can set reasonable restrictions on those variables therefore, public instance variables should not be part of a good interface definition, they cannot be defined in Java interfaces. However, the public static final modifier is used to define constants in Java, provided that the constants that need to be defined are immutable, so that they can be exposed directly to the public interface of the class. This is why the variable must be static in the interface (also must be public and final) from this translation: http://forum.java.sun.com/thread.jspa?threadid=741407& messageid=4251167 Personal Understanding: The reason for public:public is needless to say, the interface defines all the parts that are available to the user. Final: Variables belong to the details of the class, should be hidden, public variables, in the class is not a good design, does not embody the closed-closed principle of encapsulation static:final is used to define constants, only final in the class static section can also be changed, So it has to be static. Final by-Laws: Class C {static final int X; static {x = n}} interface I {int y; static {y = 17;}}

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.