Java Reference
In-Depth Information
that takes a purchase price, calculates sales tax, and returns the new price. If the
current implementation multiplies by 8% to figure the tax, and you'd like to make
the tax rate a method parameter for added flexibility, select the 8% value and
then execute Refactor | Introduce Parameter or press Ctrl+Alt+P . You're asked
for a name, and IDEA corrects all the references.
Converting an instance method to a static method
You can use the command Refactor | Make Method Static to convert an instance
method to a static one. This does more than just change the method signature. If
the method you're converting references any fields or instance variables, these
aren't available to a static method. Instead, they must be passed into the method
as parameters. IDEA asks you to provide parameters for these references, as
shown in figure 9.22. Alternatively, you're given the option of passing in the
instance object itself as a parameter, in which case the method gets the values it
needs directly from the instance.
When you're converting a static method to an instance method, you can now con-
trol the scope of the generated instance variables, making them private, public,
and so on.
Creating a new method from a code block or an expression
The Refactor | Extract Method command ( Ctrl+Alt+M ) turns a selected block
of code (or even an expression) into a method. The selected area is replaced by a
call to the newly created method. The new method may become void or may
return a value. It may also acquire parameters. This depends on the variables that
Figure 9.22
The Make Method Static refactoring has
the added side effect of translating all
internal class references into method
parameters.
 
 
 
 
 
 
 
 
Search WWH ::




Custom Search