Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor Parameters - Rule of Thumb

Tags:

In general, what is the maximum number of parameters a class constructor should accept? I'm developing a class that requires a lot of initialization data (currently 10 parameters). However, a constructor with 10 parameters doesn't feel right. That leads me to believe I should create a getter/setter for each piece of data. Unfortunately, the getter/setter pattern doesn't force the user to enter the data and without it characterization of the object is incomplete and therefore useless. Thoughts?

like image 228
javacavaj Avatar asked Apr 08 '09 14:04

javacavaj


People also ask

How many parameters should a constructor have?

This method has four parameters: the loan amount, the interest rate, the future value and the number of periods.

What is the parameters of constructor?

Parameterized constructorsA parameterized constructor accepts parameters with which you can initialize the instance variables. Using parameterized constructor, you can initialize the class variables dynamically at the time of instantiating the class with distinct values.

What are the rules for constructor?

Rules to be rememberedA constructor does not have return type. The name of the constructor is same as the name of the class. A constructor cannot be abstract, final, static and Synchronized. You can use the access specifiers public, protected & private with constructors.

How many maximum parameters we can use with difficult constructor?

Sometimes overloaded constructors are also used. But Checkstyle points out that the maximum number of arguments given to a method or constructor must be 7.

How many constructor parameters should be included in a class?

Constructor parameters should include as many as necessary to define the dependencies/inputs for the class. If the class is reduced to have one job in life, then your constructor parameters will probably be correct. As others have said, there is no hard rule on this, it really depends.

What are the rules about constructors in Java?

In this article, you will learn some rules regarding constructors in Java. We all know that an object is an instance of a specific class. To create an object, we use the new keyword, for example: Here, we create a new Dog object and specify its name is ‘Rex’. The String ‘Rex’ is passed as an argument to the following constructor of the Dog class:

Is constructor with too many parameters a code smell?

It is well know that if your class have a constructor with many parameters, say more than 4, then it is most probably a code smell. You need to reconsider if the class satisfies SRP. But what if we build and object that depends on 10 or more parameters, and eventually end of with setting all those parameters through Builder pattern?

Why do Constructors need more than 2 arguments?

I agree - in general, I think that constructors needing more than 2-3 arguments are usually a sign that a class has gotten too big, and should be refactored into smaller parts. I have had a couple of instances where many parameters were required, but it's a rare, fringe case.


1 Answers

With that many parameters, it's time to consider the Builder pattern. Create a builder class which contains all of those getters and setters, with a build() method that returns an object of the class that you're really trying to construct.

Example:

public class ReallyComplicatedClass {     private int int1;     private int int2;     private String str1;     private String str2;     // ... and so on     // Note that the constructor is private     private ReallyComplicatedClass(Builder builder) {         // set all those variables from the builder     }     public static class Builder {         private int int1;         private int int2;         private String str1;         private String str2;         // and so on          public Builder(/* required parameters here */) {             // set required parameters         }         public Builder int1(int newInt) {             int1 = newInt;             return this;         }         // ... setters for all optional parameters, all returning 'this'         public ReallyComplicatedClass build() {             return new ReallyComplicatedClass(this);         }     } } 

And in your client code:

ReallyComplicatedClass c = new ReallyComplicatedClass.Builder()         .int1(myInt1)         .str2(myStr2)         .build(); 

See pages 7-9 of Effective Java Reloaded [pdf], Josh Bloch's presentation at JavaOne 2007. (This is also item 2 in Effective Java 2nd Edition, but I don't have it handy so I can't quote from it.)

like image 67
Michael Myers Avatar answered Oct 13 '22 01:10

Michael Myers