Using Constructor of Class in Java

In Java, a constructor is a specific block of code designed to initialize a freshly instantiated object. While it might appear similar to an instance method in Java, it's unique due to the absence of a return type. By nature, Java offers a default constructor for each class. Yet, when you deliberately craft a constructor in your class, the compiler prevents generating the default one. Constructors cannot adopt abstract, static, final, or synchronized attributes. The name of the constructor should mirror the class name. It's feasible to have several constructors within a class, differentiated by their parameter lists - a concept termed constructor overloading. The "this" keyword inside a constructor alludes to the present object instance. In this Java Constructor Example, we construct a class, employ constructor overloading, and then display the outcome. Click Execute to run the Java Class Constructor Example online and see the result.
Using Constructor of Class in Java Execute
public class Dog {
    private String breed;
    private int age;

    public Dog(String breed, int age) {
        this.breed = breed;
        this.age = age;

    public void displayInfo() {
        System.out.println("Breed: " + breed + ", Age: " + age);

    public static void main(String[] args) {
        Dog dog = new Dog("Golden Retriever", 3);

Updated: Viewed: 49 times