All variables in the interface are public static final. So you can write:
Public static final int I = 10;
Or
Int I = 10; (part can be omitted)
Note that the initial value must be assigned to the variable during the declaration.
Explanation:
First, you need to understand the meaning of the interface. an interface provides a unified 'protocol', and its attributes also belong to members of 'protocol. they are public, static, and final constants. it is equivalent to a global constant.
Abstract classes are incomplete classes, which are equivalent to an intermediate layer between interfaces and specific classes. They satisfy the abstraction of interfaces and specific implementations.
If the interface can define variables, but the methods in the interface are abstract, the interface cannot modify attributes through behaviors. Some may say that it doesn't matter. You can modify the attributes of an interface by implementing the behavior of an interface object. Of course there is no problem, but consider this situation. If interface A has a static variable named a with the public access permission. According to the Java semantics, we can access variable A without implementing the interface object, and change the value of variable A in the interface through a. A = xxx. Just as this can be done in an abstract class, all objects implementing interface a will automatically own the value of a after this change, that is to say, a is changed in one place, the value of a in all these objects also changes. What is the difference between this and abstract classes? How does it reflect a higher abstract level of interfaces? How does it reflect the unified protocol provided by interfaces? What is the need for abstract interfaces? Therefore, there is no variable in the interface. If there is a variable, it is in conflict with the idea of unified abstraction provided by the interface. Therefore, the attribute in the interface must be a constant and can only be read but cannot be modified. In this way, a unified attribute can be provided for the object implementing the interface.
In layman's terms, what you think is to change is put in your own implementation, not in the interface. The interface is just a high-level abstraction of the attributes and behaviors of a class of things. It is open to modifications and extensions (different implementations of implements). interfaces are a manifestation of the open and closed principle.
All variables in the interface are public static final. So you can write:
Public static final int I = 10;
Or
Int I = 10; (part can be omitted)
Note that the initial value must be assigned to the variable during the declaration.
Explanation:
First, you need to understand the meaning of the interface. an interface provides a unified 'protocol', and its attributes also belong to members of 'protocol. they are public, static, and final constants. it is equivalent to a global constant.
Abstract classes are incomplete classes, which are equivalent to an intermediate layer between interfaces and specific classes. They satisfy the abstraction of interfaces and specific implementations.
If the interface can define variables, but the methods in the interface are abstract, the interface cannot modify attributes through behaviors. Some may say that it doesn't matter. You can modify the attributes of an interface by implementing the behavior of an interface object. Of course there is no problem, but consider this situation. If interface A has a static variable named a with the public access permission. According to the Java semantics, we can access variable A without implementing the interface object, and change the value of variable A in the interface through a. A = xxx. Just as this can be done in an abstract class, all objects implementing interface a will automatically own the value of a after this change, that is to say, a is changed in one place, the value of a in all these objects also changes. What is the difference between this and abstract classes? How does it reflect a higher abstract level of interfaces? How does it reflect the unified protocol provided by interfaces? What is the need for abstract interfaces? Therefore, there is no variable in the interface. If there is a variable, it is in conflict with the idea of unified abstraction provided by the interface. Therefore, the attribute in the interface must be a constant and can only be read but cannot be modified. In this way, a unified attribute can be provided for the object implementing the interface.
In layman's terms, what you think is to change is put in your own implementation, not in the interface. The interface is just a high-level abstraction of the attributes and behaviors of a class of things. It is open to modifications and extensions (different implementations of implements). interfaces are a manifestation of the open and closed principle.