سؤال

I'm working on a Java project on NetBeans.

At some point, I have several variables and methods (say, about twenty) which are not static. I want to refactor all of them to be static. How can I proceed with that? Can netbeans do that?

Example:

public void method1() {
//...
}
public void method2() {
//...
}

When I click somewhere, or press some buttons, I want that the previous methods become:

Example:

public static void method1() {
//...
}
public static void method2() {
//...
}

All at once.

--

Another common variant: suppose that I have set all my methods to public, but I don't want it anymore. How could I refactor all the methods to be private (so later I can change to public only what I find necessary)?

هل كانت مفيدة؟

المحلول 2

One year later I've found out that this task is better accomplished with the help of a good general-purpose text-editor, such as emacs or vim, instead of IDEs (NetBeans, Eclipse, etc).

In this case, the better text editor is the one which the user is already used to, Emacs in my case. Multiple cursors package easily solves this. Another option is to use the native query-replace function (bounded to M-% by default).

نصائح أخرى

The use case for this sort of thing is vanishingly small. Converting a static API to an OO one might be more common, but converting everything to static is something that just isn't commonly enough done for any IDE to have an automated refactoring for it that I'm aware of.

I'd question the wisdom of doing this at all, but if all your methods are already written in a static fashion, you can simply search-and-replace "public " with "public static ".

As for changing the access level of methods, it's going to vary by IDE, but again search-and-replace is likely to be much easier than any clicky checkbox refactoring dialog, especially if you're simply opening access (narrowing it may break other code, though it's unlikely to be anything a refactoring tool can fix).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top