The Extract Method refactoring creates method for a fragment of your code.
For example, you could select a block of code and press &shortcut:ExtractMethod; (Refactor | Extract -> Method):
This would result in the following: