Search code examples
javaeclipseautomated-refactoring

Eclipse/Java: How to detect and refactor instance methods that should be static?


I want to refactor a java class source code (with Eclipse) coded like a pure non-object language: there is only instance methods (public or private) but some of them should be static (no dependency with instance fields or other methods).

Is Eclipse able to detect it and refactor the code, i.e. refactor some "public" methods to "private static" ?


Solution

  • According to the page on Eclipse's Java Compile Errors/Warnings Preferences, you can do it by enabling the "Method can be static" code option, which is ignored by default.

    When enabled, the compiler will issue an error or a warning for methods which are private or final and which refer only to static members.

    After recompiling you would get a list of instance methods in your code that can be refactored as static methods.