How to refactor variable type in Eclipse?

29,431

Solution 1

How I usually do is that I change one of the upstream variables to long. This causes Eclipse to give error from each assignment where you offer long instead of int. Then I just select ctrl-1 (Quick-fix) on each of them and pick "change variable x to long".

This works when the new type is not directly assignable to the old one.

Solution 2

I don't think you can do this directly using the refactoring tool in Eclipse. However, what you can do is place your cursor over the variable x and hit CTRL + SHIFT + G to find all references in your workspace. This will then allow you to change any dependent references.

Solution 3

IntelliJ has this feature (refactor->type migration), but unfortunately, Eclipse doesn't have it yet. I hope Eclipse will add it soon.

Share:
29,431
Stepan Yakovenko
Author by

Stepan Yakovenko

Hello! I'm developing commercial software since age of 16. I am working remotely in international teams since 2011. I have many years of Java/JS/C++/web development experience and also interested in scientific (numerical calculations) work. If you have remote part-time job, please send me an email: [email protected]. Only 100% remote opportunities considered. My resume is available here. #SOreadytohelp

Updated on July 09, 2022

Comments

  • Stepan Yakovenko
    Stepan Yakovenko almost 2 years

    I want to refactor code like this:

    int x=4;
    int y=x;
    System.out.println(y);
    

    How can i do this automatically in Eclipse so x's type promotion to long would cause dependent variables to change their types also:

    long x=4;
    long y=x;
    System.out.println(y);
    

    ?