Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ShortCut for adding class field to existing constructor in AndroidStudio or IDEA

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

like image 948
mohax Avatar asked Jan 09 '16 11:01

mohax


People also ask

Why we use constructor in Android?

A constructor is a special method that is called whenever an object is created using the new keyword. It contains a block of statements that is used to initialize instance variables of an object before the reference of this object is returned by new.

How do you implement a constructor from another class in Java?

To call one constructor from another constructor is called constructor chaining in java. This process can be implemented in two ways: Using this() keyword to call the current class constructor within the “same class”. Using super() keyword to call the superclass constructor from the “base class”.

What is constructor in Java?

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created.


1 Answers

  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
like image 79
mohax Avatar answered Nov 09 '22 23:11

mohax