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

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

WBOY
WBOYforward
2023-08-19 23:05:171515browse

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);
}

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete