7

I have a question about local classes in Java (classes that declares in the method or in blocks bounded by { }).

Is there any reason not to declare local class as final? We cannot inherit other class from local class (if it's not defined at the same scope), but when we declare it as final, maybe compiler can make code much simpler?

Thank you!

animuson
  • 53,861
  • 28
  • 137
  • 147
user197284
  • 281
  • 1
  • 3
  • 7
  • What do you mean by "classes that declares in the method or in blocks bounded by { })"? Are you talking about inner classes or just new instance declarations? Please give us an example of what you mean. (And welcome to StackO!) – Guillaume Nov 15 '11 at 13:44
  • if "We cannot inherit other class from local class" what is the reason/advantage for declaring it final? – user85421 Nov 15 '11 at 14:09
  • 1
    @Guillaume He's talking about local classes. In terms of nested classes, you've got static nested classes, non-static nested classes (called "inner classes"), local classes and anonymous classes. Local classes are defined within a method body or code block and only usable within that scope. – G_H Nov 15 '11 at 14:09
  • OK, got it now - I've never used a local class before :) – Guillaume Nov 15 '11 at 14:24

4 Answers4

6

People seem to be a bit confused about anonymous classes and local classes. This is a local class:

public void m() {
   class MyClass{}
   MyClass cl = new MyClass();
}

You can declare MyClass final, but it is actually possible to inherit from it, so as anywhere else in Java can declare it final to avoid this:

public void m() {
   class MyClass{}
   MyClass cl = new MyClass();
   class MyOtherClass extends MyClass{}
   MyOtherClass cl2 = new MyOtherClass();
}

To my knowledge, anonymous classes are not considered final. However, syntactically there is no way to inherit from them, so it would require a mighty class file hack to do so.

Mathias Schwarz
  • 7,099
  • 23
  • 28
  • Yeah, but every implementation of such class can be made without any problems without inheritance. I mean that if your method contains a large hierarchy, maybe it's a problem in design? And if the hierarchy isn't big at all, you don't need inheritance. – user197284 Nov 15 '11 at 17:16
1

There is no reason not to declare final in any case, except when you explicitly aim for inheritance.

I guess the reason for not doing so is mostly negligence as there are no noticeable practical consequences in most cases. Some code analysis tool like FindBugs warn you if a field could be declared final.

A question about the universal use of the final keyword with very good answers may be found here

Community
  • 1
  • 1
kostja
  • 60,521
  • 48
  • 179
  • 224
0

There is not a reason not to except that it is an unneeded keyword in your code. ie. there is no reason not to, and no reason to do so. If the class is declared within a method than it cannot be accessed from any other place and therefore cannot be subclassed. So why prevent something that cannot be done?

So adding the word does not change behavior in any way but does clutter your code. Someone might think "why is this class final, am I missing something fancy?"

John B
  • 32,493
  • 6
  • 77
  • 98
-2

"classes that declares in the method" are anonymous classes, and "classes that declares in blocks bounded by { }" are inner classes.

Anonymous classes cannot be declared as final because there is no way to inherit it.

Inner classes can be final unless there would be any subclasses of it.

JSPDeveloper01
  • 770
  • 2
  • 10
  • 25
  • No. You can declare a non-anonymous class within a method. It's called a local class. Rarely used, but it exists. And the term "inner class" should only be used for **non-static** nested classes. – G_H Nov 15 '11 at 14:10
  • -1 see response from Mathias Schwarz for an exampled of a local inner class and here: http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html – Puce Nov 15 '11 at 14:11