Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Eclipse, how do I change the default modifiers in the class/type template?

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.

like image 862
gustafc Avatar asked Oct 23 '09 08:10

gustafc


People also ask

What is the default modifier for a class?

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.

What is default class modifier in Java?

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() { ...

How do I change the main method in Eclipse?

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!


2 Answers

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.

like image 110
michael.kebe Avatar answered Nov 15 '22 09:11

michael.kebe


Just check the appropriate access modifier when creating the new class with the New Class Wizard.

New Java Class Wizard

like image 43
jitter Avatar answered Nov 15 '22 11:11

jitter