Interface of constant values question

Posted on 2006-04-18
Last Modified: 2010-03-31
I am trying to get the following to compile.  I am not sure how the syntax works when using an interface that only contains constant values.

The "play" method of both base class "InstrumentX" and derived class "WindX" use the constants in interface NoteX, however, since NoteX resolves to a constant int I get the following error.  

How do I remedy the situation?
C:\java\code\c08>javac play(NoteX) in InstrumentX cannot be applied to (int);
 interface NoteX {
        public static final int
                MIDDLE_C = 0, C_SHARP = 1, C_FLAT = 2;
class InstrumentX implements NoteX {
  public void play(NoteX n ) {
class WindX extends InstrumentX {
    public void play(NoteX n ) {
    System.out.println(" n )");
public class WindError {
  public static void tune(InstrumentX i) {;
  public static void main(String[] args) {
    WindX flute = new WindX();
    tune(flute);   }
} ///:~
Question by:kellyputty
    LVL 14

    Expert Comment

    by:Tommy Braas
    When you specify that a method accepts a parameter of type NoteX, you have to ensure that you pass a class instance which does just that.
    LVL 14

    Accepted Solution

    In this case you're trying to pass an int. Even though that int is defined in your interface, the int itself can never be a NoteX.

    Change your interface to an abstract class, and let the InstrumentX extend it.


     abstract class NoteX {
       private int note;
       private NoteX(int note) {
          this.note = note;
       public static final NoteX MIDDLE_C = new NoteX(0);
       public static final NoteX C_SHARP = new NoteX(1);
       public static final NoteX C_FLAT = new NoteX(2);
    LVL 14

    Expert Comment

    by:Tommy Braas

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now