Is there any shortcut in Eclipse that allows me to add a field to the argument list of an existing constructor?
Example:
I hava this class:
public class A {
int a;
int b;
public A(int a, int b) {
this.a = a;
this.b = b;
}
}
when i add a field int c
(or many fields) i want to add it to the argumentlist of the constructor and assign the parameter to the field:
public class A {
int a;
int b;
int c; //this is new
public A(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
}
i currently do this by creating the parameter manually and then press CTRL + 1
and then choose "assign parameter to field"
but if i add more than one field at once this isn't really a good solution imho.
I don't want to create a new constructor!
To generate constructor(s) from a superclass, just press Alt+Shift+S, C (or alternatively select Source > Generate Constructor from Superclass… from the application menu). A dialog pops up allowing you to select the constructor(s) you'd like to create.
To get System. out. println() line in eclipse without typing the whole line type sysout and press Ctrl + space.
Default Constructor - a constructor that is automatically created by the Java compiler if it is not explicitly defined.
I would use the "change method signature" refactoring first (option+command+c on mac) to add the extra parameter(s) to the constructor. This way existing code that calls the constructor can pass sensible default as parameters (if you like). Then choose as many times CTRL+1 to quick fix the new fields into the class as you suggested.
To use this shortcut in Intellij the variable must be final and private.
Ex: private final String name;
In MAC System the shortcut is Option+Return, i believe in windows is Alt+Enter.
Then Click in Add constructor parameter.
Have fun!
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