Some Useful Code Refactoring Techniques Available In Eclipse
1. RENAMING
So this feature allows you to rename variables, classes, methods, packages, folders, and almost any Java identifier. When you rename an identifier, all references to that identifier are also renamed. The shortcut to invoke this refactoring is Alt+Shift+R. And when you invoke the shortcut on an identifier in Eclipse, a small box displays within the editor itself where you can change the identifier name. When you press Enter, all references to that identifier are changed too.
You can see here in this screenshot
2. MOVE
You can use Move to move a class, instance method, one or more static methods, static fields, types, compilation units, packages, source folders and projects and on a text selection resolving to one of these element types from one package to another. It physically moves the class to the folder corresponding to the package and also changes all references to the class to refer to the new package. The shortcut to invoke this refactoring is Alt+Shift+V. It will also ask for to update the references of that method.
See screenshot below:
3. CHANGE METHOD SIGNATURE
This allows you to change the signature of a method. It will modify all calls to that method to use the new signature. To use this refactoring, select Refactor > Change Method Signature. The dialog box displays, allowing you to change everything about the method, including adding or removing parameters, changing the order of the parameters, changing the return value type, adding exceptions to the declaration of the method, and even changing the name of the method.
4. EXTRACT METHOD
This refactoring allows you to select a block of code and convert it to a method. Eclipse automatically add the method arguments and return types to that method. This is useful when a method is very large and you want to subdivide blocks of it into different methods. It could also be useful if you have a piece of code that is copied across many methods and you want to centralize that into a single method and use the same everywhere. The shortcut to invoke this refactoring is Alt+Shift+M.
Or select the block of code and right click Refactor -> Extract Method in Eclipse.
See screenshot below:
When you select for Extract method refactoring then it will ask for the method name:
Eclipse finds other occurrences of that code and replaces it with a call to the new method.
5. EXTRACT LOCAL VARIABLE
This refactoring allows you to create a new variable for the expression currently selected and replaces the selection with a reference to the new variable.
Just press ALt+Shift+L by selecting an expression inside your code.
You will see Extract local variable dialog box:
Press Ok, then you will see like this in your code
6. EXTRACT CONSTANTS
This refactoring allows you to creates a static final field from the selected expression and substitutes a field reference for it. And rewrite in other places in the entire code where the same expression occurs.
To use, select the number or string literal in the editor, press Ctrl+1 and select Extract to Constant.
7. INLINE
This will inline a selected local variable, method or constant if possible. Eclipse replaces the selection with its declaration and puts it directly into the statement.
For example:
If the selection is possible to inline the variable, Eclipse will ask to confirm and you will see a confirmation box like this:
Click OK to proceed, and you will see the results
You can invoke this refactoring technique by using Alt + Shift + I shortcut.