Java Reference
In-Depth Information
Moving inner classes up
The
Move
refactoring has one final trick. You can use it to move an inner class to
the upper level, where it's moved to its own file that
IDEA
creates for you. To acti-
vate this type of refactoring, put your cursor on the class definition line of the
inner class you wish to move, and execute a
Move
refactoring. Doing so brings up
yet another variation of the
Move
refactoring dialog, as shown in figure 9.17.
This refactoring also gives you the option of passing in a reference to the orig-
inal containing class as a parameter to the new class's constructor. Any references
to the original class instance are appropriately remapped if you select this option.
9.4.4
Working with fields, variables, and constants
Next we'll discuss refactoring operations that deal with manipulating fields, vari-
ables, and constants used in your project.
Converting literal values to a class constant
Have you ever had to work on someone else's code (or code you wrote yourself a
long time ago) and encountered code similar to the following?
int delay = lastreboot * 3600000;
user.setRoleId(64);
Where did that
3600000
come from, and what does it mean? Is role
ID
64 an
administrator or a regular user? Sometimes numbers like these are referred to as
magic numbers
; they're magic in the sense that they must have importance and
meaning—but, magically, that meaning has disappeared. Magic numbers make
programs difficult to read, harder to maintain, and more likely to be buggy. For
example, what if someone changes the numbers assigned to the roles
ID
s? Will
role 64 still make sense? It's always a good idea to avoid magic numbers while
programming, and
IDEA
offers a solution to this problem.
Figure 9.17
The Move refactoring can also refactor an
inner class into an upper class.