I found the following example in one of the java forum.
interface employee{
class Role{
public String rollname;
public int roleId;
public Object person;
}
Role getRole();
// other methods
}
I have executed the above code snippet and it is compiling successfully.
Which means we can have a class inside an interface.
My question is what is the use of having such classes? Is it any design pattern?
This code snippet kind of answers your question already. The class
Roleis used by theemployeeinterface ingetRole()method. The designer of the interface decided that this class is so tightly coupled with the interface that it is worth to define it inside that interface to emphasize how important that class is for the interface.Also it provides semantic namespace for the class:
employee.Role. However I see this kind of construct for the first time while static classes defined inside other classes are quite common (for the same purposes as above).