Home > Java > javaTutorial > body text

Why are interface variables in Java static and final by default?

WBOY
Release: 2023-08-19 23:05:17
forward
1558 people have browsed it

Why are interface variables in Java static and final by default?

An interface defines a protocol of behavior and not how we should be implemented. A class that implements an interface follows the protocol defined by the interface.

  • Interface variables are static because Java interfaces cannot be instantiated individually. The value of the variable must be assigned in a static context where no instance exists.
  • The final modifier ensures that the value assigned to the interface variable is a true constant cannot be re-assigned. In other words, interfaces can declare only constants, not instance variables.

Template :

interface interfaceName{
   // Any number of final, static variables
   datatype variableName = value;
   // Any number of abstract method declarations
   returntype methodName(list of parameters or no parameters);
}
Copy after login

The above is the detailed content of Why are interface variables in Java static and final by default?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template