22

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 + 1and 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!

neun24
  • 222
  • 2
  • 10
Philipp Sander
  • 10,139
  • 6
  • 45
  • 78

8 Answers8

9

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.

Jurgen Vinju
  • 6,393
  • 1
  • 15
  • 26
  • 1
    of course it works :-) and yes, if Eclipse had anything better for your task it could be simpler... just imagine if we could write such tools with ease and add them as plugins like a breeze. – Jurgen Vinju Nov 18 '13 at 19:09
  • 1
    Shift-Alt-C on Windows. This is one of my favorite refactoring tools, and probably Eclipse's most powerful. – Thomas W Jun 09 '20 at 03:27
8

To use this shortcut in Intellij the variable must be final and private.

  1. Declare your variable as final.

Ex: private final String name;

  1. In MAC System the shortcut is Option+Return, i believe in windows is Alt+Enter.

  2. Then Click in Add constructor parameter.

Have fun!

Rafael Filipake
  • 114
  • 1
  • 4
5

Add a parameter to the constructor and press alt+Enter(Control Assist) and you will get an option to create field for parameter then press Enter. This is available in latest IntelliJ 2017.2.

Rakesh
  • 4,004
  • 2
  • 19
  • 31
1

The only short command in that area, that I'm aware of, of is "generate constructor using fields", which is available when pressing Alt+S. Maybe this could be a little bit of help. Here is some further useful info on shortcuts.

http://www.vogella.com/articles/EclipseShortcuts/article.html

user1186155
  • 238
  • 2
  • 10
0
  1. Copy any additional logic in the old constructor to the clipboard (none in your example).
  2. Delete the old constructor.
  3. Create new constructor with ALT+insert -> constructor -> CTRL+A -> ENTER
  4. Paste any additional logic copied in step 1.
-1

You can try this

  1. Right click the java class
  2. Source->Generate Constructor using fields.
  3. Select super constructor to use and instance variables to add to the constructor.
Kamlesh Arya
  • 4,864
  • 3
  • 21
  • 28
  • good idea but this will not work, because constructors with different signatures in the same class are no super-constructors – Philipp Sander Nov 12 '13 at 14:40
-1

In Eclipse:

  1. In Constructor select new parameter
  2. press ctrl+1
  3. select "Assign parameter to new field"

New field will be created in and will be set in your constructor.

Alex
  • 1
  • 4
-2

Use alt+shift+s+o. Generate Constructor using fields window will appear, click the select all button and press OK, you will get constructor with all the fields initialised.

Amuthan
  • 1,635
  • 3
  • 12
  • 12