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
Enter
directly, thenUp
andCtrl + Enter
for no-arg constructor.Enter
directly, thenCtrl + A
to select all of the arguments.
Getters and Setters
get
for generating only getters, thenCtrl + A
to select’em all.set
for generating only setters, thenCtrl + A
to select’em all.gas
for generating both getters and setters, thenCtrl + A
to select’em all.
Override toString, equals and hashCode
to
for generating thetoString
method.ha
oreq
forequals
andhashCode
, then manually pick your fields or just hitEnter
three times.
Snippets generation
Type the following inside of your class then press Enter to automatically generate the desired snippet:
psvm
for a public static void main method.sout
for 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 + F9
to compile a single class.Ctrl + Shift + F10
to run themain
method.
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 stepBuild
at the bottom of the window by clicking on the red minus button and clickOK
; - Back to your code, type
Ctrl + Shift + F9
to compile it; - Finally, type
Ctrl + Shift + F10
to 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!