DED9

Complete Tutorial For Builders In Java (In Simple Language)

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

What is a manufacturer?

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

The Java compiler distinguishes between method and constructor using the name and type of recursive. In Java, the constructor has a class-like name and does not return any 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 the Java constructor does not accept any parameters, the constructor is no-arg. 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

If you do not create the constructor yourself, the Java compiler will automatically create an argument-free constructor at runtime. This builder is known as the default builder. The default constructor initializes each of the informal sample variables.

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.

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

Overload builders in Java

Similar to method overload, you can have two or more manufacturers with the same name with different 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

Exit mobile version