Java Keywords (Part XVIII): The static keyword
Java keyword list
abstract | continue | for | new | switch |
assert | default | goto* | package | synchronized |
boolean | do | if | private | this |
break | double | implements | protected | throw |
byte | else | import | public | throws |
case | enum | instanceof | return | transient |
catch | extends | int | short | try |
char | final | interface | static | void |
class | finally | long | strictfp | volatile |
const* | float | native | super | while |
static
and mentioned that I would leave for later discussion another use of that keyword. Well, the time has come.
Using static
method in interfaces
Let me start by saying that the only reason why I decided to write a separate, short article for this topic is the same reason for writing the previous article on the use of the default
keyword: I didn't want to make the original article on static modifier to be longer than it needed to be. In this specific case, while the use of the keyword is the same in interface methods and class methods, I think it is still a good idea to write this separately.
As you may recall, the primary reason for classes to include static contents is to ensure all instances of the class contain the same data (in the case of static fields) or display identical behavior (in the case of methods). In the case of static interface methods, using the keyword public
is not necessary since interface methods are inherently public. So, the code snippet below illustrates a simple example of using the keyword static
in an interface method.
public interface FordVehicle {
static void showBrand() {
System.out.println("Ford");
}
To learn more, visit the Oracle Java page on interface static methods.
Next up, Part XIX: The assert
keyword
Comments
Post a Comment