简体   繁体   中英

Reasoning for making a method final

Sorry, quick question here, just found something in my notes I don't understand in relation to making a method final. My notes claim you should make a method final for this reason :

Makes it impossible to enforce invariants.

A String should behave as a String.

I don't really understand what is meant by this. Can someone please break it down for me ? Thanks a lot.

I would guess that should have said "Make it possible to enforce invariants". Basically, if someone can override a method, then they can change behaviors affecting your class's invariants.

There are typically two reasons to make a method final, performance and design. When a method is final, it may be inlined. Before HotSpot compiling (JDK 1.1.x), these methods were usually inlined at compile time, whereas with HotSpot they are inlined at runtime, unless the compiler can guarantee that the inlined method will always be compiled together with the code that uses it. There are two reasons I know for making a local variable or a parameter final. The first reason is that you don't want your code changing the local variable or parameter. It is considered by many to be bad style to change a parameter inside a method as it makes the code unclear. As a habit, some programmers make all their parameters "final" to prevent themselves from changing them. I don't do that, since I find it makes my method signature a bit ugly.

The second reason comes in when we want to access a local variable or parameter from within an inner class. This is the actual reason, as far as I know, that final local variables and parameters were introduced into the Java language in JDK 1.1. Source

Yeah basically making anything final means you cannot change it. By this same reasoning, one would make a method final within a class to make it impossible for other subclasses to override this method's functionality. This is applicable to situations where you would absolutely want this one function to always to do this one thing.

Your notes might make more sense if they read:

A final method makes it possible to enforce invariants.

In other words, finalizing a method ensures that it will return the value that you mean it to, instead of returning an overridden value that someone else mistakenly thought it should be.

Those notes don't make much sense - I think impossible should be 'possible'. The idea is that if you don't want subclasses to change the behavior of a method, you can mark the method as final and it will not be able to be overridden. This gives finer grained control than marking the entire class as final.

Basically if you make a method final, then you can't override that method in a subclass.

If the behaviour of your application relies on a certain method behaving exactly in a certain way and you don't want other developers to come along and change this behaviour, then you can use the 'final' keyword.

A String is unmodifiable upon its creation. What that means is that all its internal fields are declared final, and it provide no method to update its internal contents. Therefore, when you declare a string as "i am string", you know that the string you declared will hold that value until the end of the time.

A method should be declared final when you don't want the method to be overridden by a class that extends yours. A class that has only final methods can still be mutable if any of these methods change the variables inside the class.

I like using unmodifiable classes, it does help me to know the state of my program at any time and in a way it prevent bugs that would be hard to catch.

    class A{
        public A(){
            print("A");
        }

        public void print(String t){
            System.out.println(t);
        }
    }

    class B extends A{
        public B(){
            super();
        }

        @Override
        public void print(String t){
            System.out.println("Not A: " + t);
        }
    }

    new B();

Prints "Not A: A"

If the super class method "print(" were final this type of bug could not happen.

Via final you can enforce this method remaining unchanged so that A can always be happy printing "A"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM