The recent learning of multi-threading and the writing of the Philosopher's dining problem (Dining philosophers) requires defining a global variable, the number of philosophers. A common practice is to define a static final variable in one of the classes, and then have the other class access him through the class name. Here, you want to use the design idea of the first version of the application layer protocol of the previous training project, even if you use an interface class to define the variables that all subclasses will use. Then, a problem with the interface member variable and static final is drawn.
(a) A simple question
First, look at a section of code:
1 //Variable.java2 Public InterfaceVariable {3 Public intNum_philosophers = 5;4 }5 6 //Diningphilosophers.java7 Public classDiningphilosophersImplementsvariable{8 Public Static voidMain (string[] args) {9lock[] Chopsticks =NewReentrantlock[num_philosophers];Ten System.out.println (chopsticks.length); One } A}
In the above code, the static method of the Diningphilosophers class directly uses the public int variable in the interface, will there be an error?
(ii) analysis
There may be someone's first reaction is not possible, because static methods cannot directly use non-static member variables of the class. Some of my friends think so too (well, don't rule out my misleading).
In fact, the above procedure is not a problem .
(1) First, the static method does not directly use the non-static member variables of the class. I have written a similar post, "To learn Java solution no enclosing of type * is accessable" You can refer to it.
(2) Second, all interface member variables must be public static final (explained later), so we can omit some modifiers when writing the code, so the above num_philosophers even if the declaration statement is int NUM _philosophers=5; It is still a public static final variable (so, this also explains, if we do not assign the initial value, why the error). We can use the JAVAP tool to view the compilation information for the variable class:
(3) Finally explain why the member variable of interface must be public static final.
I found a relatively good article on Google "Why does we have the only public static final variables in interfaces?", the following is probably translated (change):
The interface defines the protocol for the behavior, not how the behavior executes the implementation. The class that implements the interface supports the behavior protocol defined in the interface.
All the fields declared in the interface are public static final. Why?
- If a variable is not defined as final, the implementation of any class can change the value of the variable. At the same time he becomes part of the realization of the class, and the interface is a pure specification without any implementation;
- If the variable is static, then the variable belongs to the interface, not to the instance object or to the runtime object. (Note: Because the interface cannot be instantiated, it is not meant to be defined as non-static.) );
- The interface defines how the caller interacts with an instance object of an interface's implementation class, so if the member is not public, then the caller is not able to approach it;
The Declaration of each field of an interface must be pubic static final, which allows us to specify all or part of the modifiers at the time of declaration. At the same time, the declaration of any field must have an initialization expression, it can not necessarily be a constant expression, its calculation and assignment will only occur once, and then this interface field is initialized (note: This is a very interesting feature, interested to chat with:-)).
Re-learn the member variables of Java interface