Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto create constructor body and fields in Netbeans based on signature

Tags:

java

netbeans

If I start from an empty class (TestClass) in Netbeans and add the following empty constructor:

public void TestClass(String a, String b) {
}

is there a way to have netbeans automagically generate:

private final String a;
private final String b;

public void TestClass(String a, String b) {
    this.a = a;
    this.b = b;
}

I know that I can first create the 2 members and ask netbeans to auto-generate the constructor but I'm asking for the other way round.

For example, in eclipse, this can be achieved by pressing CTRL+1 on the constructor's argument > assign parameter to new field.

like image 732
assylias Avatar asked Jul 02 '12 20:07

assylias


2 Answers

from: https://coderwall.com/p/oyanzg

Just point the cursor at the point where you want the generated code to appear, and press Alt + Insert (or select Source -> Insert Code). The following menu will appear, where you can choose to generate whatever you want:

enter image description here

like image 68
Ali Avatar answered Oct 18 '22 02:10

Ali


You can write the empty constructor with the required signature. Then set the cursor next to a parameter and press Alt+ENTER.

NetBeans will ask to create a new field. Press ENTER and NetBeans will write the code for you.

I think you have to do it for each parameter separately, but I'm not sure.

Generally, Alt+ENTER in NetBeans is similar to Ctrl+1 in Eclipse, also at other places.

like image 44
Puce Avatar answered Oct 18 '22 03:10

Puce