Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a void method in IntelliJ IDEA?

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?

like image 820
lichengwu Avatar asked Sep 22 '12 12:09

lichengwu


People also ask

How do I extract a 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.

How do I get method definition in IntelliJ?

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).

How do I override a method in IntelliJ?

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).


Video Answer


2 Answers

To create a new method from usage in the code like:

... someMethodName() ... 

AltEnter on the red code:

Create Method

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 psvmTab.

like image 124
CrazyCoder Avatar answered Sep 18 '22 06:09

CrazyCoder


IntelliJ IDEA 15

Generate a main method

  • Default:

    Type psvm (public static void main) > press Tab

  • Use the template from Eclipse (main instead of psvm)

    • File > Settings or press Ctrl + Alt + S

      File > Settings

    • Editor > Live Templates

      Editor Live Templates

    • From the right side, click on the "+" sign > Live Template

      Add > Live Template

    • Add the following details:

      • Abbreviation: main
      • Description: main() method declaration
      • Template text:

        public static void main(String[] args){   $END$ } 

        main

        You will see the new template added in Others.

    • Click on Define

      Define

    • Select Java > Press on OK

      Select Java

    • 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() or m(1,2) > Press Alt + Enter > Click on "Create method ..." (or press Enter if it is already selected)

    New method

like image 40
ROMANIA_engineer Avatar answered Sep 19 '22 06:09

ROMANIA_engineer