Declaring Fields in an Interface - Java Object Oriented Design

Java examples for Object Oriented Design:Field

Introduction

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

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

It is recommended not to use 'public, static, and final' when declaring fields in an interface.

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

Demo Code

interface MyInterface {
  int YES = 1;//from   w  w w .  j  a  va2s.  c  o m
  int NO = 2;

}

public class Main {
  public static void main(String[] args) {
    System.out.println("MyInterface.YES = " + MyInterface.YES);
    System.out.println("MyInterface.NO = " + MyInterface.NO);
  }
}

Result


Related Tutorials