Home » Java programming language

Java Constructors with Examples

In this tutorial, we are going to learn about the constructors in Java, what is constructor in java? How many types of constructors? What is the purpose of constructor?
Submitted by Preeti Jain, on June 11, 2019

Java Constructor

  • A constructor is a special function or method to initialize an object when it is created.
  • Constructor name must be the same as the class name.
  • The syntax of the constructor is similar as method except constructors have no explicit return type.

Purpose of Constructor

The purpose of a constructor is to assign initial values to the instance variables at runtime.

Types of Constructors

There are two types of constructor in java and the name of those constructors given below:

  1. Default Constructor or No argument Constructor
  2. Parameterized Constructor

1) Default Constructor or No argument Constructor

Here we will discuss in detail...

  • Default Constructor or No argument constructor is that constructor which takes no argument.
  • In this constructor, we don’t pass any argument or parameter.
  • When we don’t include any constructor in the class so java compiler calls this constructor by default that is the name of this constructor is default constructor.

Syntax:

    class Classname{
        Classname(){
            // initialization of instance variable
        }
    }

Example:

import java.util.*;

// Class Declaration
class Constr {
    // Declaring str instance variable of String type
    String str;
    // Constructor Definition
    Constr() {
        str = "Hello World!!";
    }
}

public class Main {
    public static void main(String[] args) {
        // Constructor Calling
        Constr con = new Constr();
        System.out.println(con.str);
    }

}

Output

D:\Programs>javac Main.java
D:\Programs>java Main
Hello World !!

2) Parameterized Constructor

Here we will discuss in detail...

  • Parameterized Constructor are those constructor which takes argument explicitly.
  • In this constructor we must pass argument or parameter.
  • When we include both constructor (Default and Parameterized) in the class so java compiler does not call this constructor bydefault (i.e. this constructor will be called explicitly).

Syntax:

    class Classname{
        Classname(datatype arg1, datatype arg2, ...){
            // initialization of instance variable
        }
    }

Example:

import java.util.*;

// Class Declaration
class ParamConstr {
    // Instance Variable
    String str;
    // Constructor Definition
    ParamConstr(String stri) {
        str = stri;
    }
}

public class Main {
    public static void main(String[] args) {
        // Calling Parameterized Constructor
        ParamConstr pcon = new ParamConstr("Hi, Welcome in parametrized constructor");
        System.out.println(pcon.str);
    }
}

Output

D:\Programs>javac Main.java

D:\Programs>java Main
Hi, Welcome in parameterized constructor





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.