18

Is there a way to make classes final by default in Eclipse? i.e: on save actions, or in the create new class dialog?

GNUnit
  • 577
  • 5
  • 9
  • Just curious, why would you want to default all classes to final? The only time I could see this being useful is if you are creating a framework/public API... in which case there should be too many classes which are exposed publicly. Though maybe there is another situation where this is warranted?? – DigitalZebra Jun 18 '11 at 16:51
  • 2
    is it really too much to check a checkbox? -- come on now... – mre Jun 18 '11 at 16:51
  • 2
    @Polaris878, Because inheritance is rarely useful. @mre: Yes, because I would have to hit it every time I make a class. I'm using an IDE because I want to get thins done faster. – GNUnit Jun 19 '11 at 00:40
  • 2
    Why was my question voted down? It's perfectly clear. And why do people keep voting up something that's not an answer to it? – GNUnit Jun 21 '11 at 13:00
  • I'd love to be able to do this as well. It would be excellent to make an own template that could be distributed to all our teams so that all classes by default were final. Sadly, it seems it cannot be done easily today. – K Erlandsson Oct 09 '12 at 09:17

2 Answers2

5

This truly is a hack, but what you can do in your eclipse settings is set the following code template:

Explained below

Under Java > Code Style > Code Templates > Code > New Java Files, you can add the final keyword right before ${type_declaration}.

The caveat is that the final checkbox is no longer properly functional, but if you are someone that blows through the New Class dialog before clicking a box in the first place, then it may be a lesser evil to remove final in the actual code afterwards in the 10% of cases you need it.

The other caveat is that the first line of the class will look like final public MyClass; while still valid, it is not the more idiomatic public final MyClass.

Alex Gittemeier
  • 5,224
  • 30
  • 55
3

Click on final check box in new class dialog.

enter image description here

Also take a look at this: How to create a custom 'new class wizard' for Eclipse?

Community
  • 1
  • 1
Harry Joy
  • 58,650
  • 30
  • 162
  • 207