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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.