In Eclipse, when I type main
ctr+space, it will generate a static void main
method for me. And when I type methodName
ctr+space, smart code completion will suggest generating the method named methodName
.
How can I auto-generate a void method in IntelliJ?
To extract method:Press Ctrl+Alt+M or from the main menu, select Refactor | Extract | Method. In the dialog that opens, configure a method options, such as visibility, parameters, and so on. You can also change a name of the method if you need. Click OK.
In IntelliJ IDEA, you can see where and how symbols, such as tags, classes, fields, methods, or functions are defined in your project. For this purpose, the IDE features the Quick Definition popup. To view definition of a symbol, select it in the editor and press Ctrl+Shift+I (or click View | Quick Definition).
Override a method of a superclassOn the Code menu, click Override methods Ctrl+O . Alternatively, you can right-click anywhere in the class file, then click Generate Alt+Insert , and select Override methods. Select the methods to override (hold the Shift or Ctrl key to perform a multiple select).
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.
main
methodDefault:
Type psvm
(public static void main) > press Tab
Use the template from Eclipse (main
instead of psvm
)
File > Settings or press Ctrl + Alt + S
Editor > Live Templates
From the right side, click on the "+" sign > Live Template
Add the following details:
main
main() method declaration
Template text:
public static void main(String[] args){ $END$ }
You will see the new template added in Others.
Click on Define
Select Java > Press on OK
Type main
in your Java code > press Tab
void
methodType your method name followed by parentheses (+ the arguments, if you use them) - E.g.: m()
or m(1,2)
> Press Alt + Enter > Click on "Create method ..." (or press Enter if it is already selected)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With