I try to automatically generate a copy constructor with IntelliJ, but I can not select this due to the box is grey out. How do I automatically generate a copy constructor by IntelliJ?
My Java Class
public class Parameter {
public double mA;
public double mB;
public double mC;
public double mD;
public String mPath;
public Parameter(double mA, double mB, double mC, double mD, String mPath) {
super();
this.mA = mA;
this.mB = mB;
this.mC = mC;
this.mD = mD;
this.mPath = mPath;
}
public Parameter(double mA, double mB, double mC, double mD) {
this(mA, mB, mC, mD, "");
}
public double getA() {
return mA;
}
public double getB() {
return mB;
}
public double getC() {
return mC;
}
public double getD() {
return mD;
}
public String getPath(){
return mPath;
}
}
The button is grey out.
You can use ⌘N (macOS), or Alt+Insert (Windows/Linux) for the Generate menu and then select Constructor , Getter , Setter or Getter and Setter .
Ctrl+O enables you to create default methods for methods you have to O verride. Alt+Insert enables you to generate commons methods (getters, setters, constructors etc...) If you call a method that does not exist, Alt+Enter opens a list of quick fixes.
A copy constructor in a Java class is a constructor that creates an object using another object of the same Java class. That's helpful when we want to copy a complex object that has several fields, or when we want to make a deep copy of an existing object.
What you can do is use "create constructor", select all the fields, then manually "tweak" that into a copy constructor :)
No plugin required anyway, and still mostly usable :)
Generating a copy constructor functionality is not built into IntelliJ IDEA. Perhaps that Generate Copy Constructor action is coming from a plugin?
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