This post shows how to create a Java class constructor with fields faster in Eclipse. Before the advent of smart IDEs, Java developers write out almost all codes as it was the only way to do and IDEs were in their infancy. Then, fast forward to now! We have Eclipse, and IntelliJ to name a few.
Java Class with many fields
Consider the following Java class with many fields. Although not typical in small applications, such classes do exist, especially in large programs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | package com.turreta.eclipse; public class CrazyHugeJavaBean { private String field1; private String field2; private String field3; private String field4; private String field5; private String field6; private String field7; private String field8; private String field9; private String field10; private String field11; private String field12; private String field13; private String field14; private String field15; private String field16; private String field17; private String field18; private String field19; private String field20; public String getField1() { return field1; } public void setField1(String field1) { this.field1 = field1; } // ... public void setField19(String field19) { this.field19 = field19; } public String getField20() { return field20; } public void setField20(String field20) { this.field20 = field20; } } |
The Java class has twenty instance variables that we may need to initialize. Back in the day, we would write the getter and settings method one by one. Meanwhile, if we needed a constructor that accepts many fields, we would write out the arguments for the constructor.
Use Eclipse to generate Java class constructors with fields
For this post, we use Eclipse. However, we could use the same principle in IntelliJ or other smart IDEs. First, we need to have Eclipse running and create a Java class with many fields. Then, right-click anywhere in the source code file and choose Source. Next, choose Generate Constructor using Fields... option, as shown below.
Then, a window will display from which you can select the fields you wish to include in the constructor.
Finally, click OK. Imagine doing this manually!
Eclipse Creates a Java Constructor
When we go back to our Java class, we can see a new constructor with selected fields.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | package com.turreta.eclipse; public class CrazyHugeJavaBean { ... private String field20; public CrazyHugeJavaBean(String field1, String field2, String field3, String field4, String field5, String field6, String field7, String field8, String field9, String field10, String field11, String field12, String field13, String field14, String field15, String field16, String field17, String field18, String field19, String field20) { super(); this.field1 = field1; this.field2 = field2; this.field3 = field3; this.field4 = field4; this.field5 = field5; this.field6 = field6; this.field7 = field7; this.field8 = field8; this.field9 = field9; this.field10 = field10; this.field11 = field11; this.field12 = field12; this.field13 = field13; this.field14 = field14; this.field15 = field15; this.field16 = field16; this.field17 = field17; this.field18 = field18; this.field19 = field19; this.field20 = field20; } public String getField1() { return field1; } ... } |
There are many other similar features Eclipse has. For example, Eclipse can generate hashCode and equals methods. The same goes for other smart IDEs.