Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Interface - constants and static class in normal interface

Tags:

java

Consider following interface.

public interface ThirdPartyApiHandler {

    public OperationResult doOperation(OperationInput input);

    public static class OperationResult {
         //members of OpeationResult. metrics after file processing
         private int successfulRecords;
         private int failedRecords;
    }  

    public static class OperationInput {
         //implementations call third party API to process this file.
         private String inputBatchFile; 
    }  

    //Constant which would be same across all implementations.
    public static final int GLOBAL_CONSTANT = 1;
}

Is above interface a bad design?

  1. OperationResult and OperationInput are defined as static class. They would be only used by implementations and not anywhere else. Advantage that I see here is - I don't have to create separate files for these two classes. Also they get namespace of parent class.

  2. I have read about constant interface. But in this case, I am defining constant in normal interface which are bound to be same across all implementations and would be used in those implementations.

I am using this pattern for first time so wanted to get suggestions.

like image 364
RandomQuestion Avatar asked Aug 14 '13 09:08

RandomQuestion


People also ask

Are static constants in an interface legal in Java?

In Java syntactically it is possible and allowed to define constants in both Interface and Class. In old days putting constants in Interface was common practice because by default defined constants are marked public static final and constants can be used in implementation class without interface name prefix.

Can you have static constants in an interface?

The value of the variable must be assigned in a static context in which no instance exists. The final modifier ensures the value assigned to the interface variable is a true constant that cannot be re-assigned. In other words, interfaces can declare only constants, not instance variables.

Should constants be in interface or class?

Java programmers commonly define constants inside their interfaces, if it makes design sense. You can do so using variables in an interface because the values will be present instantly at runtime and their values shared among all classes implementing your interface, because they are static and final.

Can I use interface for constants?

It's possible to place widely used constants in an interface. If a class implements such an interface, then the class can refer to those constants without a qualifying class name. This is only a minor advantage. The static import feature should always be considered as a replacement for this practice.


2 Answers

OperationResult and OperationInput are defined as static inner class. They won't be used anywhere else.

That's OK since they will not be used anywhere else. If they're long than I would prefer to have them in separate classes.

I have read about constant interface. But in this case, I am defining constant in normal interface which are bound to be same across all implementations and would be used in those implementations.

That's a good place to declare such a field.

like image 184
Tala Avatar answered Oct 17 '22 10:10

Tala


Having nested classes in interfaces is only matter of additional namespace. This approach help to organize the code when small interfaces are created to support simple data structure.

I recommend you this lecture: Java Tip 75: Use nested classes for better organization.

Note that public and static are redundant in this case so you do not need them. What you need to remember is that having such classes do not limit other developers to use them in other parts of code.

From my point of view, this is a good design but, i would extend and replace the class with interfaces.

public interface ThirdPartyApiHandler {

    OperationResult doOperation(OperationInput input);

    interface OperationResult {
         int getSuccessfulRecords();
         int getFailedRecords();
    }  

    interface OperationInput {
         String getInputBatchFile(); 
    }  

    final int GLOBAL_CONSTANT = 1; //This could be replaced by enum but no need 
}
like image 42
Damian Leszczyński - Vash Avatar answered Oct 17 '22 10:10

Damian Leszczyński - Vash