Internal interfaces?

I am new to Java and I don’t understand what this structure is. I know what an interface is and how it is defined, but in this case I really don't know. Could you talk about what?

public interface WebConstants { public interface Framework { String ACTION = "action"; } public interface Look { String LIST_CONT = "list.cont"; } } 
+4
source share
2 answers

Each field inside the interface is implicitly open, static, and final. In this case, WebConstants declares an internal interface (open, static and final) Framework and (public, static and final) Look , which also have some (public, static and final) string fields.

This is a (not very general) way of ordering constants in your code, with this structure you can enter:

 String action = WebConstants.Framework.ACTION; String list = WebConstants.Look.LIST_CONT; 

The advantage of this is that since WebConstants is an interface, you cannot accidentally initiate it

+1
source

You might want to look through the listings if you use a similar solution:

 public enum WebConstants { ACTION("action"), LIST_COUNT("list.count"); private String display; private WebConstants(String display) { this.display = display; } public String getDisplay() { return display; } } 

So you can use it by calling WebConstants.ACTION.getDisplay ().

Having a constant interface doesn't really make any sense to me. The best way to do things might be to have abstract access methods.

 public interface ActionAware { public String getAction(); } public interface ListCountAware { public String getListCount(); } public abstract class AbstractUseCase implements ActionAware, ListCountAware { public void doSomething() { String action = getAction(); String listCount = getListCount(); } } public final class SpecificUseCase extends AbstractUseCase { public final static String ACTION = "specific.action"; public final static String LIST_COUNT = "specific.list.count"; public String getListCount() { return LIST_COUNT; } public String getAction() { return ACTION; } // other methods } 
+1
source

All Articles