Convert a instance member function which uses this references, to static function
-
15-06-2021 - |
Question
I have a large non static JAVA function, which uses this reference at several places inside it. I want to convert it to static function and pass "this" reference as a parameter, so that I can take it to utility class.
Is there any tool which can automate this process?
UPDATE: Please share if some one has his own quick way of doing this(without using any tool).
Solution
Let the method myMethod
reside in the class Foo
, and you need to make it static in the class Util
.
- Declare
private Foo foo;
inUtil
. - In Eclipse, use the Move refactoring on
myMethod
, chooseUtil
as the target. - Add the
static
modifier to the moved method and fix the accesses to the addedFoo foo
's internals. - Remove the
private Foo foo;
fromUtil
.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow