Last Updated on 15/11/2020
Here follows a short list of some IntelliJ IDEA useful shortcuts and tips to improve your productivity whilst coding Java.
Code generation
Type Alt + Insert to open the code generation dropbox, then type:
Constructors
Enterdirectly, thenUpandCtrl + Enterfor no-arg constructor.Enterdirectly, thenCtrl + Ato select all of the arguments.
Getters and Setters
getfor generating only getters, thenCtrl + Ato select’em all.setfor generating only setters, thenCtrl + Ato select’em all.gasfor generating both getters and setters, thenCtrl + Ato select’em all.
Override toString, equals and hashCode
tofor generating thetoStringmethod.haoreqforequalsandhashCode, then manually pick your fields or just hitEnterthree times.
Snippets generation
Type the following inside of your class then press Enter to automatically generate the desired snippet:
psvmfor a public static void main method.soutfor an empty System.out.println() call.
Shortcuts
- Auto format:
Ctrl + Alt + L. - Organise imports:
Ctrl + Alt + O. - Introduce local variable:
Ctrl + Alt + V. - Open type:
Ctrl + N. - Open type exclusive to your project:
Ctrl + Shift + N. - Search everywhere: 2x
Shift, it opens a dialog where you can type things like format and the IDE will search for files and actions containing format in its name.
Execution
Running the main method
Ctrl + Shift + F9to compile a single class.Ctrl + Shift + F10to run themainmethod.
Running the main method without compiling the whole project
When you run a main method of a class within your project, it invokes the Build process of the given project. To avoid that:
- Go to the dropdown menu at the left side of the green play button and select
Edit Configurations...; - On
Application -> Class-you-want-to-run, remove the stepBuildat the bottom of the window by clicking on the red minus button and clickOK; - Back to your code, type
Ctrl + Shift + F9to compile it; - Finally, type
Ctrl + Shift + F10to run your main method.
Code templates
If you still don’t use Lombok, the following templates are useful for auto generating code and javadocs. The highlighted lines are the lines I’ve changed from the original templates.
Getter method
#if($field.modifierStatic)
static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
#if ($field.boolean && $field.primitive)
is##
#else
get##
#end
${name}() {
return $field.name;
}
Setter method
#set($paramName = $helper.getParamName($field, $project))
#if($field.modifierStatic)
static ##
#end
void set$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))($field.type $paramName) {
#if ($field.name == $paramName)
#if (!$field.modifierStatic)
this.##
#else
$classname.##
#end
#end
$field.name = $paramName;
}
I will keep updating this list as soon as I discover new features and shortcuts.
Cya!