Constructors In Java – Three Different Types Of Constructors

What Is Constructors In Java

The Constructors In Java is a special method that is used to initialize or set up new objects.

When a class object is created, the Constructor is called.

Why Constructors Are Used In Java

The Use Of Constructors In Java is to initialize objects. I will further discuss Constructor In Java.

When an object is created, it is set up by a constructor. It has the same name as its class and looks like a method in terms of how it is put together.

But constructors don’t say what type of return they give.

Most of the time, you’ll use a constructor to set the initial values for instance variables that the class defines, or to do any other start-up tasks that are needed to make a fully formed object.

No matter if you define a constructor or not, all classes have them because Java has a default constructor that sets all member variables to zero.

Once you define your own constructor, however, the default one is no longer used.

Syntax For Constructor In Java

Example Syntax:

class StudentName {
    StudentName() {
       }
}

Types Of Constructors

There are only 3 types of constructors, namely No Argument Constructors, Parameterized Constructors, and Default Constructors.

Java No Argument Constructors

The No Argument Constructor is a constructor that doesn’t take any parameters.

If we don’t define a constructor in a class, the compiler will create one for us (with no arguments).

1. No Argument Constructors

Similar to methods, a Java constructor may or may not have parameters (arguments).

If a constructor doesn’t accept any parameters, it is called a “no-argument” constructor. As one example.

private NoArgConstructor() {
   // body of the constructor
}

2. Java Private No Argument Constructors

public class Main {

  int i;

  // constructor with no parameter
  private Main() {
    i = 10;
    System.out.println("Constructor is called");
  }

  public static void main(String[] args) {

    // calling the constructor without any parameter
    Main obj = new Main();
    System.out.println("Value of i: " + obj.i);
  }
}
Output
Constructor is called
Value of i: 10

In the example above, we made a constructor called Main (). In this case, the constructor doesn’t take any arguments.

This is why it is called a “no-arg constructor.” Notice that the constructor has been set to be private.

Once a constructor is marked as private, no one outside the class can use it.

So, you can’t use the private constructor to make objects from outside the class.

Here, the object is made within the same class. It is Because of this, the program can get to the constructor.

Visit Java Implement Private Constructor if you want to find out more.

But if we want to make objects outside of the class, we must make the constructor public.


In order for you to test the Java code provided in this lesson, you must test the code in your code editor.

But if you don’t have it, you can download it. Here’s the link NetBeans IDE But if you wish to run this code online, we also have an online compiler in Java.

You can test the above example here! ➡Java Online Compiler 

3. Java Public No Argument Constructors

class Company {
  String name;

  // public constructor
  public Company() {
    name = "PIES";
  }
}

public class Main {
  public static void main(String[] args) {

    // object is created in another class
    Company obj = new Company();
    System.out.println("Company Name = " + obj.name);
  }
}

Output:
Company Name = PIES

Java Parameterized Constructors

The Parameterized Constructors let you set up the instance variables with parameters.

Using a parameterized constructor, you can give the class variables different values when you create a new instance of the class.

It accepts one or more parameters as well. These kinds of builders are called parameterized builders (constructors with parameters).

1. Parameterized Constructors

public class Main {

  String languages;

  // constructor accepting single value
  Main(String lang) {
    languages = lang;
    System.out.println(languages + " is Handsome");
  }

  public static void main(String[] args) {

    // call constructor by passing a single value
    Main obj1 = new Main("Glenn");
    Main obj2 = new Main("Jude");
    Main obj3 = new Main("Adones");
  }
}

Output:
Glenn is Handsome
Jude is Handsome
Adones is Handsome

In the above example, we created a constructor called Main ().

Here, the constructor takes one parameter. Observe the phrase,

Main obj1 = new Main("Glenn");

Here, we’re sending the single value to the constructor.

In the constructor, the language variable is initialized based on the argument.

Java Default Constructor

The Default Constructor In Java sets the class’s data members to their default values, like 0 for int, 0.0 for double, etc.

If the user doesn’t implement a specific constructor for the class, the Java compiler uses this one instead.

If we don’t make any constructors, the Java compiler will make a no-arg constructor for us as the program runs.

This is called a “default constructor.”

1. Java Default Constructor

public class Main {

  int a;
  boolean b;

  public static void main(String[] args) {

    // A default constructor is called
    Main obj = new Main();

    System.out.println("Default Value:");
    System.out.println("num = " + obj.a);
    System.out.println("Decision = " + obj.b);
  }
}

Output:
Default Value:
num = 0
Decision = false

You can test the above example here! ➡Java Online Compiler 

We haven’t made any constructors so far. So, the Java compiler makes the default constructor by itself.

Any instance variables that haven’t been set up yet are given default values by the default constructor.

TypeDefault Value
booleanfalse
byte0
short0
int0
long0L
char\u0000
float0.0f
double0.0d
objectReference null

In the program above, the default values for the variables num and Decision are 0 and false, respectively.

The program above is the same as:

public class Main {

  int a;
  boolean b;

   Main() {
    a = 0;
    b = false;
  }

  public static void main(String[] args) {
    // call the constructor
    Main obj = new Main();

    System.out.println("Default Value:");
    System.out.println("num = " + obj.a);
    System.out.println("Decision = " + obj.b);
  }
}

Output:
Default Value:
num = 0
Decision = false

As you can see, the program has the same result as the first program of the Default Constructors.


Important Notes of Java Constructors

  • When you create an object, you automatically call it a constructor.

  • When making a constructor, there are two rules to follow. The name of the constructor should be the same as the name of the class. A Java constructor can’t return anything.

  • If a class doesn’t have a constructor, the Java compiler creates one automatically when the class is run. The default constructor sets the default values for instance variables. For instance, the int variable will start out with a value of 0.

  • Constructors Type
  1. No Argument Constructors – a constructor that doesn’t accept any arguments
  2. Parameterized Constructors – a constructor that takes arguments
  3. Default Constructors – a constructor that is made by the Java compiler even if it is not defined explicitly.
  • A constructor can’t be either static, final, or abstract.
  • A constructor can be used in more than one way, but it can’t be changed.

Constructors Overloading

We can make two or more constructors with different parameters, just like we can do with Java methods. This is called overloading constructors.

1. Java Constructors Overloading
public class Main {

  String language;

  // constructor with no parameter
  Main() {
    this.language = "PHP";
  }

  // constructor with a single parameter
  Main(String language) {
    this.language = language;
  }

  public void getName() {
    System.out.println("The Best Programming Language in the world is : " + this.language);
  }

  public static void main(String[] args) {

    // call constructor with no parameter
    Main obj1 = new Main();

    // call constructor with a single parameter
    Main obj2 = new Main("Python");

    obj1.getName();
    obj2.getName();
  }
}

Output:

The Best Programming Language in the world is : PHP
The Best Programming Language in the world is : Python

In the above example, there are two constructors: Main() and Main (String language).

In this case, both of the constructors set the value of the variable language to something different.

During the creation of an object, different constructors are called and different values are given depending on the parameter.

Conclusion

In this article, we talked about Constructors In Java, how it works, and how this chapter could help you learn Java.

This could be one of the most important learning guides that will help you improve your skills and knowledge as a future Java Developer.

What’s Next

The next section talks about Datatypes in Java programming. At the end of the session, you’ll know what datatypes are all about in Java.


Leave a Comment