Interface Variables (Fields) Tutorial

An interface may contain a very specific type of a variable. When a variable (field) is declared in an interface it essentially becomes an implicitly declared constant. Here are the rules that apply to a variable in an interface:

  • Interface variables must be initialized when they are declared.
  • Interface variable values cannot be changed after they are declared.
  • The only modifiers that can be explicitly applied to an interface variable are: public, static, or final.
  • All interface variables are implicity marked public, static, and final.
  • Don't be fooled by what appears to be Default (Package-Private) access, public is the only access modifier allowed and implicitly required.


Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md InterfaceVariables
C:\Java>cd InterfaceVariables
C:\Java\InterfaceVariables>Notepad InterfaceVariables.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class InterfaceVariables {
    public static void main(String args[]) {
        CheesyOnScreenKeyboard cosk = new CheesyOnScreenKeyboard();
        cosk.displayKeyPressed(2, 1);
        cosk.displayKeyPressed(1, 1);
        cosk.displayKeyPressed(3, 1);

        System.out.println("Look, you can directly access an interface variable: " + DigitalKeypad.AKey );
    }
}

interface DigitalKeypad {
    String AKey = "A"; // Looks like a default access variable, correct?
    public String BKey = "B"; 
    public static String CKey = "C";
    public static final String D_KEY = "D";
    // protected String EKey = "E"; // NO - the only modifiers that can be explicitly applied to interface variable are: public, static, or final.
    // int i;
    // i = 10; // NO - Interface variables must be initialized when they are declared.
    int i = 10; // YES initialized and declared.
    // i = 20; // NO - Interface variable values cannot be changed after they are declared.


    String keyPress(int xCoord, int yCoord);
}

class CheesyOnScreenKeyboard implements DigitalKeypad {

    @Override
    public String keyPress(int xCoord, int yCoord) {
        if (xCoord == 1 && yCoord == 1 )
            return AKey;
        else if (xCoord == 2 && yCoord == 1 ) {
            return this.BKey;
        } else if (xCoord == 3 && yCoord == 1 ) {
            return DigitalKeypad.CKey;
        } 
        return "";
    }

    void displayKeyPressed(int xCoord, int yCoord) {
        System.out.println("You pressed " + keyPress(xCoord, yCoord) );
    }
  
}



Now switch back to the command prompt (CMD) and type in javac InterfaceVariables.java and press Enter.
Now type in java InterfaceVariables and press Enter.


C:\Java\InterfaceVariables>javac InterfaceVariables.java
C:\Java\InterfaceVariables>java InterfaceVariables
You pressed B
You pressed A
You pressed C
Look, you can directly access an interface variable: A
    

Final thoughts

Sometimes in Java it matters more what is not there versus what is there. There is a lot of controversy surrounding interface variables. I'm not really a fan of interface variables, but it is important to understand how they work and the rules that apply to them.


Tutorials