Final arguments in interface methods - what's the point?

前端 未结 5 1892
甜味超标
甜味超标 2020-11-28 03:26

In Java, it is perfectly legal to define final arguments in interface methods and do not obey that in the implementing class, e.g.:

public inter         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 03:49

    Update: Original answer below was written without fully understanding the question, and therefore does not directly address the question :) Nevertheless, it must be informative for those looking to understand the general use of final keyword.

    As for the question, I would like to quote my own comment from below.

    I believe you're not forced to implement the finality of an argument to leave you free to decide whether it should be final or not in your own implementation.

    But yes, it sounds rather odd that you can declare it final in the interface, but have it non-final in the implementation. It would have made more sense if either:

    a. final keyword was not allowed for interface (abstract) method arguments (but you can use it in implementation), or
    b. declaring an argument as final in interface would force it to be declared final in implementation (but not forced for non-finals).


    I can think of two reasons why a method signature can have final parameters: Beans and Objects (Actually, they are both the same reason, but slightly different contexts.)

    Objects:

    public static void main(String[] args) {
        StringBuilder cookingPot = new StringBuilder("Water ");
        addVegetables(cookingPot);
        addChicken(cookingPot);
        System.out.println(cookingPot.toString());
        // ^--- OUTPUT IS: Water Carrot Broccoli Chicken ChickenBroth 
        //      We forgot to add cauliflower. It went into the wrong pot.
    }
    
    private static void addVegetables(StringBuilder cookingPot) {
        cookingPot.append("Carrot ");
        cookingPot.append("Broccoli ");
        cookingPot = new StringBuilder(cookingPot.toString());
        //   ^--- Assignment allowed...
        cookingPot.append("Cauliflower ");
    }
    
    private static void addChicken(final StringBuilder cookingPot) {
        cookingPot.append("Chicken ");
        //cookingPot = new StringBuilder(cookingPot.toString());
        //     ^---- COMPILATION ERROR! It is final.
        cookingPot.append("ChickenBroth ");
    }
    

    The final keyword ensured that we will not accidentally create a new local cooking pot by showing a compilation error when we attempted to do so. This ensured the chicken broth is added to our original cooking pot which the addChicken method got. Compare this to addVegetables where we lost the cauliflower because it added that to a new local cooking pot instead of the original pot it got.

    Beans: It is the same concept as objects (as shown above). Beans are essentially Objects in Java. However, beans (JavaBeans) are used in various applications as a convenient way to store and pass around a defined collection of related data. Just as the addVegetables could mess up the cooking process by creating a new cooking pot StringBuilder and throwing it away with the cauliflower, it could also do the same with a cooking pot JavaBean.

提交回复
热议问题