Variables in Interfaces

We can use interface to organize constants.

  

interface MyConstants {
  int NO = 0;
  int YES = 1;
}

class Question implements MyConstants {
  int ask() {
      return YES;
  }
}

public class Main implements MyConstants {
  static void answer(int result) {
    switch (result) {
      case NO:
        System.out.println("No");
        break;
      case YES:
        System.out.println("Yes");
        break;
    }
  }
  public static void main(String args[]) {
    Question q = new Question();
    answer(q.ask());
  }
}  

The output:


Yes
Home 
  Java Book 
    Class  

Interface:
  1. What is a Java Interface
  2. Implementing Interfaces
  3. Accessing Implementations Through Interface References
  4. Partial interface Implementations
  5. Variables in Interfaces
  6. Interfaces Can Be Extended