IntelliJ IDEA 2017.3 Help

Generating Delegation Methods

On this page:

Introduction

You can create methods that delegate behavior to the fields or methods of your class. This approach makes it possible to give access to the information of a field or method without direct exposing this field or method.

Creating delegation methods

To create a delegation method

  1. Do one of the following:
    • On the main menu, choose Code | Generate.
    • Right-click the editor and choose Generate on the context menu
    • Press Alt+Insert.
  2. In the pop-up list that is displayed in the editor, select Delegate Methods. Select Target To Generate Delegates For dialog appears, displaying the list of objects to be delegated to.
  3. Select the target field or method, and click OK. The Select Method To Generate Delegation For dialog appears, displaying the list of methods to delegate.
  4. Select the desired methods. For multiple selection, use Ctrl and Shift keys. Click OK.

Example

Currency class has a field calendar of the type Calendar. To gain access to certain functionality of the Calendar class from the Currency class, we need to create a new method that will delegate the request to calendar.

Calendar calendar; public int get(int i) { return calendar.get(i); }
Last modified: 6 March 2018