How to generate a void method in IntelliJ IDEA?
To create a new method from usage in the code like:
...
someMethodName()
...
AltEnter on the red code:
It's also possible to type void methodName()
and use Complete Statement (CtrlShiftEnter), it will become:
void methodName() {
|
}
You could create your own Live Template as @Makoto answered, but programming by intention seems to be more natural. When you don't have a method, you write code that will use it, then create the method from the intention action - this way IDEA will generate the method signature automatically according to the parameters and return type in the not yet existing method usage, like String result = someMethod(stringParam);
.
Finally, it is worth nothing that in IntelliJ IDEA main()
method can be generated using psvm
Tab.
IntelliJ IDEA 15
Generate a main
method
Default:
Type
psvm
(public static void main) > press TabUse the template from Eclipse (
main
instead ofpsvm
)File > Settings or press Ctrl + Alt + S
Editor > Live Templates
From the right side, click on the "+" sign > Live Template
Add the following details:
- Abbreviation:
main
- Description:
main() method declaration
Template text:
public static void main(String[] args){ $END$ }
You will see the new template added in Others.
- Abbreviation:
Click on Define
Select Java > Press on OK
Type
main
in your Java code > press Tab
Generate a void
method
Type your method name followed by parentheses (+ the arguments, if you use them) - E.g.:
m()
orm(1,2)
> Press Alt + Enter > Click on "Create method ..." (or press Enter if it is already selected)