Java Interface static methods

Introduction

A static method defined by an interface can be called independently of any object.

static interface methods are not inherited by either an implementing class or a sub interface.

Here is the general form:

InterfaceName.staticMethodName 
interface MyIF {/*from   www  .ja v  a 2  s  .c o m*/
  // This is a "normal" interface method declaration.
  int getNumber();

  // a default method.
  default String getString() {
    return "Default String";
  }

  // This is a static interface method.
  static int getDefaultNumber() {
    return 0;
  }
}

// Use the default method.
public class Main {
  public static void main(String args[]) {

    int defNum = MyIF.getDefaultNumber();

    System.out.println(defNum);
  }
}



PreviousNext

Related