Accessing Interface Variables in Java

The  variables of an interface are always declared as "final". Final variables are those variables, whose  values are constants and cannot be changed. The class that implements the interface can use the variables as declared in the interface and cannot modify or changed the value of the variable.


 interface selectcolor
int blue=4;
int yellow=5;
int pink=6;
public void choose(int color);
class selectimp implements selectcolor
public void choose(int color)
case blue:
   System.out.println("The color selected is blue");
case yellow:
   System.out.println("The color selected is yellow");
case pink:
   System.out.println("The color selected is pink");
public static void main(String aa[])
int a1,b1,c1;

selectimp st=new selectimp();

accessible interface variables in java

In this example first  an interface selectcolor is created and the value for the integers blue, yellow, pink are set as 4,5,6.
Then a method choose() which takes an integer parameter is declared.
A class selectimp is created, which implements the interface selectcolor.
Then the method choose() of the interface selectcolor is implemented using the switch case statements.
Then there is a main() which creates the object of the class selectimp and call the choose() of the selectimp class with different parameters or arguments.

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @