Eclipse's default template for new types (Window > Preferences > Code Style > Code Templates > New Java Files) looks like this:
${filecomment} ${package_declaration} ${typecomment} ${type_declaration}
Creating a new class, it'll look something like this:
package pkg;
import blah.blah;
public class FileName {
// Class is accessible to everyone, and can be inherited
}
Now, I'm fervent in my belief that access should be as restricted as possible, and inheritance should be forbidden unless explicitly permitted, so I'd like to change the ${type_declaration}
to declare all classes as final
rather than public
:
package pkg;
import blah.blah;
final class FileName {
// Class is only accessible in package, and can't be inherited
}
That seems easier said than done. The only thing I've found googling is a 2004 question on Eclipse's mailing list which was unanswered.
So, the question in short: How can I change the default class/type modifiers in Eclipse?
I'm using Eclipse Galileo (3.5) if that matters.
Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc. A variable or method declared without any access control modifier is available to any other class in the same package.
The default access modifier is also called package-private, which means that all members are visible within the same package but aren't accessible from other packages: package com.baeldung.accessmodifiers; public class SuperPublic { static void defaultMethod() { ...
One shortcut that really helps me, Eclipse has syntax suggestions, and if you type "main" into the terminal window and hit control + space, you can choose main method and it'll populate it complete with enclosed braces. Helpful if you're not always quick to remember syntax off the top of your head!
Looks like it is not possible. The ${type_declaration}
is internal stuff.
What you can do is to click everytime the final checkbox in the "New Java Class"-Dialog. But that's not something you want to.
Just check the appropriate access modifier when creating the new class with the New Class Wizard.
New Java Class Wizard
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With