Java - Interface Field Members

Introduction

An interface can have three types of members:

  • Constant fields
  • Abstract, static, and default methods
  • Static types via nested interfaces and classes

All members of an interface are implicitly public.

Constant Fields Declarations

You can declare constant fields in an interface.

interface Choices {
        public static final int YES = 1;
        public static final int NO = 2;
}

All fields in an interface are implicitly public, static, and final.

The Choices interface can be declared as follows without changing its meaning:

interface Choices {
        int YES = 1;
        int NO = 2;
}

You can access the fields in an interface using the dot notation in the form of

<interface-name>.<field-name>

Demo

interface Choices {
        int YES = 1;
        int NO = 2;
}
public class Main {
   public static void main(String[] args) {
      System.out.println("Choices.YES = " + Choices.YES);
      System.out.println("Choices.NO = " + Choices.NO);
   }/*from  w ww.j  av a 2 s .  c  om*/
}

Result

Quiz