This recipe focuses on how the IDE handles the renaming of all elements of a project, being the project itself, classes, methods, variables, and packages.
Let's create the code to be renamed:
With the project created we will need to clear the RenameElements.java
class of the main method and insert the following code:
package renameelements; import java.io.File; public class RenameElements { private void printFiles(String string) { File file = new File(string); if (file.isFile()) { System.out.println(file.getPath()); } else if (file.isDirectory()) { for(String directory : file.list()) printFiles(string + file.separator + directory); } if (!file.exists()) System.out.println(string + " does not exist."); } }
The next step is to rename the package, so place the cursor on top of the package name, renameelements, and press Ctrl+R.
A Rename dialog pops-up with the package name. Type util under New Name and click on Refactor.
Our class contains several variables we can rename:
Let's rename the other variables:
To rename methods, perform the steps below:
Then let's rename classes:
RenameElements.java
file.And finally renaming an entire project:
Renaming a project works a bit differently from renaming a variable, since in this action NetBeans needs to rename the folder where the project is placed. The Ctrl+R shortcut is not enough in itself so NetBeans shows the Rename Project dialog. This emphasizes to the developer that something deeper is happening.
When renaming a project, NetBeans gives the developer the possibility of renaming the folder where the project is contained to the same name of the project. This is a good practice and, more often than not, is followed.
3.147.242.204