BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Constructors in Java – A Complete Guide

Last Updated: May 30, 2024 by Chaitanya Singh | Filed Under: java

Constructor is a block of code that initializes the newly created object. A constructor resembles an instance method in java but it’s not a method as it doesn’t have a return type. In short constructor and method are different(More on this at the end of this guide). People often refer constructor as special type of method in Java.

Constructor has same name as the class and looks like this in a java code.

public class MyClass{
   //This is the constructor
   MyClass(){
   }
   ..
}

Note that the constructor name is same as class name and it doesn’t have a return type.

Table of Contents

  • How does constructor work?
  • A simple constructor program in java
  • Types of Constructors