4

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" ?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Rififi
  • 121
  • 1
  • 11
  • 3
    I don't think "private static" is what you mean. Why are you trying to do this? – tddmonkey Dec 23 '14 at 14:07
  • @MrWiggles What's wrong with private static methods? – Paul Boddington Dec 23 '14 at 14:14
  • In fact, I think a method must be static if it is not dependent of any instance field (attribute or method). For me, it's just more coherent and understandable. – Rififi Dec 23 '14 at 16:53
  • Use caution with this. Static methods can be harder to refactor or reimplement later. You can't override them in a future child class, and you changing them back to instance methods later can be difficult (if they're part of an exposed API). – Ian McLaird Dec 23 '14 at 18:39
  • See this question: http://stackoverflow.com/questions/685522/using-private-static-methods – tddmonkey Dec 23 '14 at 19:22

1 Answers1

4

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523