Generating code
IntelliJ IDEA provides multiple ways to generate common code constructs and recurring elements, which helps you increase productivity. These can be either predefined or custom templates, which are applied differently based on the context, various wrappers and automatic pairing of characters. Additionally, IntelliJ IDEA provides code completion and Emmet support.
On the main menu, select
(Alt+Insert) to open the popup menu with available constructs that you can generate.Generate constructors
IntelliJ IDEA can generate a constructor that initializes specific class fields using values of corresponding arguments.
To generate a constructor for a class:
On the Code menu, click Generate (Alt+Insert).
In the Generate popup, click Constructor.
If the class contains fields, select the fields to be initialized by the constructor and click OK.
The following code fragment shows the result of generating a constructor for a class:
Generate delegation methods
IntelliJ IDEA can generate methods that delegate behavior to the fields or methods of your class. This approach makes it possible to give access to the data of a field or method without directly exposing this field or method.
To generate a delegation method for a class:
On the Code menu, click Generate (Alt+Insert).
In the Generate popup, click Delegate Methods.
Select the target field or method, and click OK.
Select the desired methods to be delegated and click OK.
The following code fragment shows the result of delegating the get(i)
method of the Calendar
class inside another class:
Generate equals() and hashCode() methods
IntelliJ IDEA can generate overrides for standard equals()
and hashCode()
methods defined in Java.lang.Object
.
The previous method returns true
, if the object passed to it as an argument is equal to the object on which this method is invoked.
The previous method returns the integer hash code value for the object on which this method is invoked.
To generate the equals() and hashCode() methods for a class:
On the Code menu, click Generate (Alt+Insert).
In the Generate popup, click equals() and hashCode().
Select check boxes if you want to accept subclasses and use getters during code generation. You can also select a velocity template from the Template list to generate the code or create a custom template. Click Next.
Select the fields that should be used to determine equality, and click Next.
Select the fields to use for calculating the hash code value. You can choose only from fields that were selected on the previous step (for determining equality). Click Next.
Select the fields that contain non-null values. This optional step helps the generated code avoid checks for null and thus improves performance. Click Finish.
The following code fragment shows the result of overriding the equals()
and hashCode()
methods:
Generate getters and setters
IntelliJ IDEA can generate accessor and mutator methods (getters and setters) for the fields in your classes. Generated methods have only one argument, as required by the JavaBeans API.
The getter and setter method names are generated by IntelliJ IDEA according to your code generation naming preferences.
Generate getters and setters for a class:
On the Code menu, click Generate (Alt+Insert).
-
In the Generate popup, click one of the following:
Getter to generate accessor methods for getting the current values of class fields.
Setter to generate mutator methods for setting the values of class fields.
Getter and Setter to generate both accessor and mutator methods.
-
Select the fields to generate getters or setters for and click OK.
You can add a custom getter or setter method by clicking and accessing the Getter/Setter Templates dialog. If a field is not in the list, then the corresponding getter and setter methods are already defined for it.
The following code fragment shows the result of generating the getter and setter methods for a class with one field var
:
Note for PHP
In the PHP context, getters and setters are generated using the PHP getter/setter file template. By default, as specified in these templates, setters are generated with the set
prefix and getters with the set
or get
prefix according to the inferred field type boolean
or con-boolean
. The prefix is the value of the ${GET_OR_IS}
variable in the default getter template. The default template is configured in the Code tab on the File and Code Templates page of the Settings / Preferences Dialog.
Generate toString()
IntelliJ IDEA can generate an override for the standard toString()
method defined in Java.lang.Object
. The generated method returns the name of the class and a list of class field names with current values of fields.
Override the toString() method for a class:
On the Code menu, click Generate (Alt+Insert).
In the Generate popup, click toString().
-
Configure the following:
Select the fields to be used for generating the
toString()
method. By default, all the available fields are selected. Click Select None to add thetoString()
method that returns only the class name.Select the desired way for generating the
toString()
method from the Templates list.Select the Insert @Override checkbox if necessary. For more information, see Generate toString() Dialog.
Click the Settings button to open the toString() Generation Settings dialog, where you can tune the behavior and add custom templates. For more information, see Generate toString() Settings Dialog.
Click OK.
The following code fragment shows the result of generating the toString()
method for a class with several fields defined:
The following code inspections are related to the toString()
method:
-
Class does not override 'toString()' method can be used to identify classes in which the
toString()
method is not defined. This inspection uses the exclude settings to ignore classes with fields that are not supposed to be dumped. An additional setting is to exclude certain classes using a regular expression matching their class name. As default, this is used to exclude any Exception classes. -
Field not used in 'toString()' method can be used to identify fields that are not dumped in the
toString()
method. For example, if you added new fields to a class, but forgot to add them to thetoString()
method. Change the severity of this inspection to show errors as warnings. This will highlight any unused fields in the editor and indicate their location as yellow markers on the scroll bar.
Custom code generation templates
Templates used for generating getters and setters, as well as equals()
, hashCode()
, and toString()
methods are written in the Velocity template language. Although you can't modify predefined templates, you can add your own custom templates to implement necessary behavior.
IntelliJ IDEA provides the following variables for Velocity templates:
The following variables can be used in templates for generating getters and setters:
Variable | Description |
---|---|
$java_version | The current version of the Java Runtime Environment (JRE). |
$class | The current class. |
$helper | Provides access to various code generation helper methods. |
$settings | Provides the ability to format names according to the current code style. |
$field | Field for which getter or setter is generated. |
The following variables can be used in templates for generating the toString()
method:
Variable | Description |
---|---|
$java_version | The current version of the Java Runtime Environment (JRE). |
$class | The current class. |
$helper | Provides access to various code generation helper methods. |
$settings | Provides the ability to format names according to the current code style. |
$fields | List of fields in the current class. |
The following variables can be used in templates for generating the equals()
method:
Variable | Description |
---|---|
$java_version | The current version of the Java Runtime Environment (JRE). |
$class | The current class. |
$helper | Provides access to various code generation helper methods. |
$settings | Provides the ability to format names according to the current code style. |
$fields | List of fields in the current class. |
$instanceBaseName | Predefined name of the object on which the |
$baseParamName | Predefined name of the |
$superParamName | The name of the parameter in the |
$checkParameterWithInstanceof | Option passed from the wizard. |
$superHasEquals | Whether the superclass has |
The following variables can be used in templates for generating the hashCode()
method:
Variable | Description |
---|---|
$java_version | The current version of the Java Runtime Environment (JRE). |
$class | The current class. |
$helper | Provides access to various code generation helper methods. |
$settings | Provides the ability to format names according to the current code style. |
$fields | List of fields in the current class. |
$superHasHashCode | Whether the superclass has |