13

I have simple class:

public class A {
    private int a;
    private int b;

    public A(int a) {
        this.a = a;

    }
}

And now I want to add int b то constructors arguments and initialize class field with new argument . So it, finally, must be:

public A(int a, int b) {
    this.a = a;
    this.b = b;
}

So, if IDEA (AndroidStudio) have shortCut for it?


P.S. Same question for Eclipse: Shortcut for adding fields to existing constructor

Community
  • 1
  • 1
mohax
  • 4,435
  • 2
  • 38
  • 85

2 Answers2

13
  1. Make sure, that field, you want to add to constructor is private
  2. Select it's name with mouse
  3. Press Alt+Enter
  4. Choose add constructor parametr
mohax
  • 4,435
  • 2
  • 38
  • 85
1

Ok Then you can do this to change .

  1. Select the Constructor.
  2. Right click inside Editor.
  3. Click on Refactor.
  4. Click on Change Signature and you will see this

enter image description here

Once you are there you can add or remove parameters and add new signature, rename , etc..,

user2503849
  • 110
  • 12
  • Thanks for yours answer, but this way you can create new constructor, but cant modify existing. – mohax Jan 09 '16 at 12:31
  • Ok got it. Here is what you can do. – user2503849 Jan 09 '16 at 12:45
  • Interesting, but this way I cant simply add existing class field to constructor and initialize it with new constructor argument. This wat I can only add argument. – mohax Jan 09 '16 at 20:13
  • You can add it using overloading option but still you might not be able to acheive your goal with it i guess. – user2503849 Jan 09 '16 at 22:27