blog posts

Builders In Java

Complete Tutorial for Builders in Java Explained Simply

In this tutorial, you will get acquainted with builders in Java and learn how to create and use builders with the help of examples.

What is a Builder?

What is a Builder?

The constructor is similar to the method (but not the technique) that is called immediately when the object is created.

The Java compiler distinguishes between a method and a constructor by their names and return types. In Java, the constructor has a class-like name and returns no values.

class Test {

Test () {

// constructor body

}

}

Here, Test () is constructive. It has the same class name and no return type.

class Test {

void Test () {

// method body

}

}

Here, Test () has the same name as the class. However, its return type is void. Hence, it is not constructive but methodical.

Example: Builder in Java

  1. class ConsMain {
  2. private int x;
  3. // constructor
  4. private ConsMain () {
  5. System.out.println (“Constructor Called”);
  6. x = 5;
  7. }
  8. public static void main (String [] args) {
  9. ConsMain obj = new ConsMain ();
  10. System.out.println (“Value of x =” + obj.x);
  11. }
  12. }

Output

Constructor Called

Value of x = 5

Here, the ConsMain () constructor is called when the obj object is created.

A constructor may or may not accept an argument.

Builder without arguments

If a Java constructor accepts no parameters, it is a no-argument constructor. Its structure is as follows:

accessModifier ClassName () {

// constructor body

}

Sample constructor without arguments

  1. class NoArgCtor {
  2. int i;
  3. // constructor with no parameter
  4. private NoArgCtor () {
  5. i = 5;
  6. System.out.println (“Object created and i =” + i);
  7. }
  8. public static void main (String [] args) {
  9. NoArgCtor obj = new NoArgCtor ();
  10. }
  11. }

Output

Object created and i = 5

Here, the NoArgCtor () constructor does not accept any parameters.

Did you notice that the creator of NoArgCtor () is private?

This is because the object is made from within the same class. Hence, it can access the manufacturer.

However, if the object is created outside of class, you must make access to that constructor public. For example:

  1. class Company {
  2. String domainName;
  3. // object is created in another class
  4. public Company () {
  5. domainName = “programiz.com”;
  6. }
  7. }
  8. public class CompanyImplementation {
  9. public static void main (String [] args) {
  10. Company companyObj = new Company ();
  11. System.out.println (“Domain name =” + companyObj.domainName);
  12. }
  13. }

Output

Domain name = programiz.com

Default builder

The Java compiler automatically creates an argument-free constructor at runtime if you do not make one. This builder is the default. The default constructor initializes each informal sample variable.

The Java compiler automatically creates an argument-free constructor at runtime if you do not make one. This builder is the default. The default constructor initializes each informal sample variable.

Example: Default builder

  1. class DefaultConstructor {
  2. int a;
  3. boolean b;
  4. public static void main (String [] args) {
  5. DefaultConstructor obj = new DefaultConstructor ();
  6. System.out.println (“a =” + obj.a);
  7. System.out.println (“b =” + obj.b);
  8. }
  9. }

The above program is equivalent to:

  1. class DefaultConstructor {
  2. int a;
  3. boolean b;
  4. private DefaultConstructor () {
  5. a = 0;
  6. b = false;
  7. }
  8. public static void main (String [] args) {
  9. DefaultConstructor obj = new DefaultConstructor ();
  10. System.out.println (“a =” + obj.a);
  11. System.out.println (“b =” + obj.b);
  12. }
  13. }

Output

a = 0

b = false

Parametric constructor

A constructor may also accept parameters. Its structure is as follows:

accessModifier ClassName (arg1, arg2,…, argn) {

// constructor body

}

Example: Parametric constructor

  1. class Vehicle {
  2. int wheels;
  3. private Vehicle (int wheels) {
  4. wheels = wheels;
  5. System.out.println (wheels + ”wheeler vehicle created.”);
  6. }
  7. public static void main (String [] args) {
  8. Vehicle v1 = new Vehicle (2);
  9. Vehicle v2 = new Vehicle (3);
  10. Vehicle v3 = new Vehicle (4);
  11. }
  12. }

Output

2 wheeler vehicle created.

3 wheeler vehicle created.

4 wheeler vehicle created.

We passed an int (number of wheels) argument to the constructor when creating the object.

Overload builders in Java

As with method overloading, you can have two or more manufacturers with the same name and parameters. For example:

  1. class Company {
  2. String domainName;
  3. public Company () {
  4. this.domainName = “default”;
  5. }
  6. public Company (String domainName) {
  7. this.domainName = domainName;
  8. }
  9. public void getName () {
  10. System.out.println (this.domainName);
  11. }
  12. public static void main (String [] args) {
  13. Company defaultObj = new Company ();
  14. Company programizObj = new Company (“programiz.com”);
  15. defaultObj.getName ();
  16. programizObj.getName ();
  17. }
  18. }

Output

default

programiz.com

important points

  • Immediately after calling the objects, the creators are called.
  • The two laws of creation are:
  • The Java constructor name must match the class name exactly.
  • A Java constructor should not have a recursive type.
  • If a class does not have a constructor, the Java compiler automatically generates a default constructor at runtime. The default constructor initializes the sample variables with the default values. For example, the variable int is equal to 0.
  • Manufacturer types:
  • No-Arg Constructor – A constructor that accepts no arguments
  • Default Builder – A builder created automatically by a Java compiler. (If not explicitly defined)
  • Parametric constructor – Used to specify specific values ​​of variables in an object
  • Creators cannot be abstract, static, or final.
  • The constructor can be defined several times.

FAQ

What is the Builder pattern in Java?

It is a design pattern used to construct complex objects step by step without using multiple constructors.

Why should developers use builders in Java?

Builders improve code readability, reduce constructor overload, and make object creation more flexible.

When is the Builder pattern most useful?

It is useful when an object has many optional parameters or complex construction logic.