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):

Extract method

This would result in the following:

Extract method