5

Can final keyword be used for a method?

1

8 Answers 8

19

Absolutely! The final keyword can be applied to just about anything, in each case meaning "you don't get to change this anymore."

Here's what it means when applied to...

a variable: You simply cannot assign the variable a new value (rendering it a constant, of course)

a method: You cannot re-implement (i.e., override) this method in a subclass

a class: You cannot define a subclass

In each case we're simply indicating: once this thing is declared, this is the last value (or implementation) you'll ever see for it.

Sign up to request clarification or add additional context in comments.

4 Comments

you should add a sub-section in your answer differentiating between the case when variable points to a primitive type vs reference type.Does this make sense ? I will then remove the -1 that I gave you .
There is no distinction between reference vs. primitive types here. I assume you're alluding to the fact that reference types may not be immutable even if a variable pointing at one is final? If so, that falls outside the scope of the original question. However, you're welcome to comment constructively in any manner you believe will enhance existing answers. As this answer is several years old and has been viewed only 528 times to date, though, I recommend focusing your efforts on more current questions.
your answer is misleading to say the least . It may have been viewed by n number of people m times but still this is misleading. Immutability takes an important part in class design . So that definitely should have been mentioned .
It certainly does! StackOverflow encourages comments that enhance existing answers and I'm sure future readers will welcome any additional clarification you'd like to add.
3

Yes, it is possible to declare a method as final. That will mean that a method cannot be overridden by its subclasses.

From The Java Language Specifications, Third Edition, Section 8.4.3.3:

A method can be declared final to prevent subclasses from overriding or hiding it. It is a compile-time error to attempt to override or hide a final method.

For more information, the Writing Final Classes and Methods page from The Java Tutorials has more information.

Comments

2

Yes.

You can make a method final

public class A {
   public static final void f() {
      System.out.println("test");
   }
}

There are typically two reasons for making a method final

  1. When a method is final, it "may" be inlined.
  2. When a method is final, the method is impossible to override.

1 Comment

A more realistic case is a final NON-static method, surely?
1

Sure can. Making it impossible to override.

Comments

0

Sure, check out The Final Word on the Final Keyword

public abstract class AbstractBase
{
    public final void performOperation()    // cannot be overridden
    {
        prepareForOperation();
        doPerformOperation();   
    }

    protected abstract void doPerformOperation();    // must override
}

Comments

0

Yes.

A final method cannot be overridden by subclasses. This is often used to prevent subclasses from altering crucial behaviors of the class.

Comments

0

As a note to the other answers. You can use final. In practice I rarely see people using it and I'm not sure why.

A lot of the code I write these days is intended for multi-threaded environments and I tend to make the class final an immutable (if its a value class) so that it is threadsafe.

The problem with marking some methods as final (and not others) is that you are stating that there is something special about that method and nothing special about the others. That's rarely what people actually mean in my experience.

If a class is intended for inheritence you need to keep it clean and keep it small to prevent unwanted side-effects. All this depends on whether you are writing code for your self and your team or whether you are writing for a wider audience - i.e. a public api on an Open Source project or a commercial project.

Comments

0

yes, final keyword can be used for a method. It will preserve the immutability. it prevents between methods from being broken. For example, suppose the implementation of some method of class X assumes that method M will behave in a certain way. Declaring X or M as final will prevent derived classes from redefining M in such a way as to cause X to behave incorrectly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.