How to generate a void method in IntelliJ IDEA?

IdeIntellij Idea

Ide Problem Overview


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?

Ide Solutions


Solution 1 - Ide

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.

Solution 2 - Ide

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

Solution 3 - Ide

  1. Type the abbreviation of the main() method template: enter image description here

2. Press the template invocation key. By default, it is Tab. The abbreviation expands to the main() method.

Solution 4 - Ide

IntelliJ makes use of Live Templates to do its code completion. It's then a matter of deciding what shorthand name you wish to use to name your void method.

Here's an example. Create a live template in Settings > Live Templates, then select the "Other" box. Hit the + on the right, then give your template a shorthand keystroke name.

Here's the one that I typed up. With the two different variables $NAME$ and $ARGS$, I can tab between them when I need to fill them in. $END$ is where the cursor ends when I'm done tabbing through the other two variables.

void $NAME$ ($ARGS$) {
    $END$
}

The shorthand name I used is pmeth. So, every time I type pmeth into IntelliJ in a Java file, then hit Tab, this method is filled in, and my cursor automatically starts at $NAME$.

Solution 5 - Ide

type psvm on the Java class and then Cntrl+Period key

Solution 6 - Ide

Just type main and a suggestion will pop up. Press enter. enter image description here

Solution 7 - Ide

Simply Type the abbreviation of the main() method template:

psvm then Enter

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlichengwuView Question on Stackoverflow
Solution 1 - IdeCrazyCoderView Answer on Stackoverflow
Solution 2 - IdeROMANIA_engineerView Answer on Stackoverflow
Solution 3 - IdeTaras MelnykView Answer on Stackoverflow
Solution 4 - IdeMakotoView Answer on Stackoverflow
Solution 5 - IdeHariView Answer on Stackoverflow
Solution 6 - IdeAfriPwincessView Answer on Stackoverflow
Solution 7 - IdeNanView Answer on Stackoverflow