As most of other modern IDEs, NetBeans supports a rename refactoring. Focus the variable, methods, class etc. you want to rename and press Ctrl+R. Alternatively you may open a context menu and choose “Refactor, Rename”. The rename not only affects your Java code. If you use JSF as Java EE frontend, then the rename affects the JSF page too. But, there are some restrictions and useful features to know.Let’s assume you have a property, which is accessed from a page.

Java:

       private String _name;
       public String getName() {
               return _name;
       }

       public void setName(String name) {
               _name = name;
       }

Page:

   <h:inputText value="#{cat.name}"/>

If you now focus “getName” and invoke the rename refactoring to name it “getNameX”, then NetBeans would rename this on the page as expected: “#{cat.nameX}”

Due to a small bug, a rename back to “getName” only affects all occurrence within your Java classes, but the pages. If used on a couple a pages, it might be a mess to find all usages and to manually correct them. In case of this problem, there is a workaround: Rename “setName” to “setNameX” also. Then, a rename to “getName” and “setName” would work fine. This bug is reported to NetBeans, and I’m sure it’s gonna being fixed soon.

The names of a property’s getter and setter methods always have to (re-)named synchronous. Usually you want to keep it synchronized with the field’s name, too. And here NetBeans offers a nice feature. Simply rename the field!

When NetBeans opens the rename dialog, tick “Rename Getters and Setters”.

RenameNetBeans would rename the field, the getter and setter and all occurrence, including those on pages.

If you are editing a page and want to rename a property, action method or other, then NetBeans sadly only displays “The Rename refactoring cannot be applied to this context”. Hopefully it will support renaming some day…

But don’t give up. Simply jump directly to the Java code (Ctrl-B, read full description [1]) and rename within the Java code.

 

[1] NetBeans and Java EE: Code navigation