When I introduced interfaces in the post Java: Interfaces - I mentioned that, variables can be declared in an interface - "Variables can be declared inside the interface, but they must be initialized and they are implicitly final and static. The variables defined in the interface can not be changed (final) and can be accessed by interface (static) itself. All the members of the interface are implicitly public."
Let's write a simple program to test this concept - There is an interface IUserName, having a prototype to get the user name and display same to the console. There is a static method in the interface, which checks if the entered user name is valid, if yes, displays the user name else it displays the message accordingly. Interface has String class type variables which are used to store the messages. The class User implements the interface IUserName.
Interface IUserName (IUserName.java)
public interface IUserName {
// String MSG;
String MSGLEN = "User name should have atleast 8 characters!!";
String MSGNUM = "User name should have atleast one numeric character!!";
String MSGDOLLAR = "User name should not have dollar sign!!";
public void getUsername();
public void displayUsername();
public static boolean isValid(String name) {
boolean isValid = true;
// MSGLEN = "User should have ateast 10 characters!!";
if(name.length() < 8) {
System.out.println(MSGLEN);
isValid = false;
return isValid;
}
if(!name.matches(".*\\d.*")) {
System.out.println(MSGNUM);
isValid = false;
return isValid;
}
if(name.matches(".*[$].*")) {
System.out.println(MSGDOLLAR);
isValid = false;
return isValid;
}
return isValid;
}
}
Class User (User.java)
import java.util.Scanner;
public class User implements IUserName {
String userName = "";
public void getUsername() {
System.out.println("Enter the user name");
Scanner sc = new Scanner(System.in);
userName = sc.next();
if(IUserName.isValid(userName)) displayUsername();
}
public void displayUsername() {
System.out.println("User name is " + userName);
}
public static void main(String args[]) {
User user = new User();
user.getUsername();
// System.out.println(IUserName.MSGDOLLAR);
// System.out.println(user.MSGDOLLAR);
}
}
There are the following requirements for the user name to be valid -
- must be more than 8 characters.
- should have at least one numeric character.
- dollar sign is not allowed.
String MSGLEN = "User name should have atleast 8 characters!!";
String MSGNUM = "User name should have atleast one numeric character!!";
String MSGDOLLAR = "User name should not have dollar sign!!";
The getUsername() method in the class User accepts a user name from the console.
public void getUsername() {
System.out.println("Enter the user name");
Scanner sc = new Scanner(System.in);
userName = sc.next();
if(IUserName.isValid(userName)) displayUsername();
}
The method getUsername() calls the static method isValid() from the interface directly to check the validity of the entered user name. (The code uses a String class method matches, which takes a regular expression as a parameter and finds out whether the String matches with the regular expression. I will write a separate post to discuss regular expressions in detail.)
- Checks whether the user name has at least 8 characters. If not, the message - User name should have atleast 8 characters!! - is printed on the console.
if(name.length() < 8) {
System.out.println(MSGLEN);
isValid = false;
return isValid;
}
- Checks whether the user name has at least one numeric character. If not, the message - User name should have atleast one numeric character!! - is printed on the console. (".*\\d.*" - \\d means any digit and .* means any character with 0 or more occurrences.)
if(!name.matches(".*\\d.*")) {
System.out.println(MSGNUM);
isValid = false;
return isValid;
}
- Checks whether the user name contains character $. If yes, the message - User name should not have dollar sign!! - is printed on the console. (".*[$].*" - [$] means single character and .* means any character with 0 or more occurrences.)
if(name.matches(".*[$].*")) {
System.out.println(MSGDOLLAR);
isValid = false;
return isValid;
}
If the user name is valid, the displayUsername() method displays the user name.
public void displayUsername() {
System.out.println("User name is " + userName);
}
Here is the output:
Enter the user name
Michael
User name should have atleast 8 characters!!
Enter the user name
MichaelJohn
User name should have atleast one numeric character!!
Enter the user name
Michael$4John
User name should not have dollar sign!!
Enter the user name
Michael8John
User name is Michael8John
Let's talk about the String variables used in the interface, we have the following observations -
If we try to declare the interface variable MSG without the initialization,
String MSG;
it gives compile time error -
Error:(4, 15) java: = expected
Conclusion - The interface variables must be initialized.
If we try to assign the new value to the interface variable MSGLEN,
MSGLEN = "User should have ateast 10 characters!!";
it gives compile time error -
Error:(15, 9) java: cannot assign a value to final variable MSGLEN
Conclusion - The interface variables are implicitly final. They can't be changed.
If we try to access the interface variables directly using the interface name, or through the instance of the class implementing the interface,
System.out.println(IUserName.MSGDOLLAR);
System.out.println(user.MSGDOLLAR);
there will be no compile time error -
Conclusion - The interface variables are implicitly static. They can be accessed through the interface directly as well as by the instance of the class implementing the interface.
NOTE: So, above code verifies that the variables declared in th interface are implicitly final and static.
The code can be accessed at Github Link.