Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

How to Configure Class and Method Comment Templates in IntelliJ IDEA
Fundamentals 6 min read

How to Configure Class and Method Comment Templates in IntelliJ IDEA

This guide explains step‑by‑step how to set up class and method comment templates in IntelliJ IDEA, including creating file and live templates, configuring parameters, adding Groovy scripts for @param and @return generation, and verifying the generated Javadoc comments for Java files.

This article provides a detailed tutorial on configuring automatic class and method comment templates in IntelliJ IDEA for Java development.

First, it shows how to add a class comment template by opening

Settings → Editor → File and Code Templates → File → Class

and inserting a Javadoc header that includes author and date placeholders.

Example class template:

/**
 * @author jitwxs
 * @date ${YEAR}年${MONTH}月${DAY}日 ${TIME}
 *
 */

Next, it explains how to create a method comment template using Settings → Editor → Live Templates. A new template group (e.g., userDefine) is created, and a live template with abbreviation * is added.

Method template text to copy:

*
 * 
 * @author jitwxs
 * @date $date$ $time$$param$ $return$
 *

The template must be associated with the Java language via Define → Java in the "Applicable contexts" section.

To make the $param$ and $return$ placeholders work, custom Groovy scripts are defined for each variable.

Param generation script:

groovyScript("def result = '';def params = \"${_1}\".replaceAll('[\\[|\\]|\\s]', '').split(',').toList(); for(i = 0; i < params.size(); i++) {if(params[i] != '')result+='* @param ' + params[i] + ((i < params.size() - 1) ? '\\r\
 ' : '')}; return result == '' ? null : '\\r\
 ' + result", methodParameters())

Return generation script:

groovyScript("return \"${_1}\" == 'void' ? null : '\\r\
 * @return ' + \"${_1}\"", methodReturnType())

The article also covers optional settings such as "Skip if defined" and explains why certain placeholders are placed on the same line.

Finally, it demonstrates the results: class comments appear automatically when a new class is created, and method comments are generated correctly for various method signatures (no parameters, single parameter, multiple parameters, with or without return values).

Q&A sections address common questions about template abbreviations, the purpose of empty asterisk lines, and the handling of return types.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
JavaIntelliJ IDEAcode documentationLive TemplatesIDE configuration

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion