Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refactor variable to use getter and setters in Eclipse (Java) [duplicate]

I know I can generate setters and getters for fields in the Eclipse source menu, but I'm very surprised it doesn't offer to replace the direct field accessors with calls to the newly created methods.

Does anyone know how to do this short of manual search and replace?

Thanks!

like image 420
Jeff Axelrod Avatar asked Jul 22 '26 21:07

Jeff Axelrod


1 Answers

There is a way. Focus on the attribute, right click. Select "Refactor" -> "Encapsulate Field".

(Edited to explain why it works, even though the menu's title doesn't imply it)

The "Encapsulate Field" dialog-box will create getters/setters for you if they don't exist, and will change field-access to go through the getters/setters. The dialog automatically detects existing getters/setters so, for the requirements that you had mentioned, that would do the trick.

like image 85
Isaac Avatar answered Jul 24 '26 14:07

Isaac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!