alexpov
alexpov

Reputation: 1188

Eclipse: How to change attr from public to private by using refactoring tools

for example:

 public class Person {

    public final int age;
   }

Is there a way in eclipse to perform the following automatic\semi automatic (by using refactoring tools)

1) change the visibility of age from public to private.

2) add getter for age

3) everywhere in the code change *.age to *.get_age() (* = instance of person class)

And iam not looking for find\replace solutions ... :)

Upvotes: 3

Views: 1962

Answers (1)

Martin Klinke
Martin Klinke

Reputation: 7332

You can use the "Encapsulate Field" refactoring. However, it will generate both getter and setter in order to preserve potential write access to the field (only if the field is not final, thanks to @Louis Wasserman). After all, a refactoring is supposed to restructure the code without modifying its function. But at least you can then proceed from there and remove the setter manually to see if there are any issues that need to be fixed.

Upvotes: 6

Related Questions