Skip to content
-
Subscribe to our newsletter & never miss our best posts. Subscribe Now!
  • https://www.facebook.com/
  • https://twitter.com/
  • https://t.me/
  • https://www.instagram.com/
  • https://youtube.com/
ICT BYTE Logo ICT BYTE

Nepal's #1 Tech Blog

ICT BYTE Logo ICT BYTE

Nepal's #1 Tech Blog

  • HOME
  • GADGETS
    • MOBILE
    • LAPTOPS
    • SMARTWATCH
    • TABLETS
  • EVENTS
  • NEPAL
    • Banking
    • B.Sc. CSIT
    • BCA
  • MCS
    • 1st Sem
      • Managerial Communication
      • Object Oriented Programming
      • Open Source Technology
      • Design and Analysis of Algorithm
    • 2nd Sem
    • 3rd Sem
    • 4th Sem
  • Utility Tools
    • .np Cover Letter Generator
    • Image Size Reducer
  • HOME
  • GADGETS
    • MOBILE
    • LAPTOPS
    • SMARTWATCH
    • TABLETS
  • EVENTS
  • NEPAL
    • Banking
    • B.Sc. CSIT
    • BCA
  • MCS
    • 1st Sem
      • Managerial Communication
      • Object Oriented Programming
      • Open Source Technology
      • Design and Analysis of Algorithm
    • 2nd Sem
    • 3rd Sem
    • 4th Sem
  • Utility Tools
    • .np Cover Letter Generator
    • Image Size Reducer
Subscribe
Close

Search

Trending Now:
nepal budget latest tech updates trends
Masters of Computer ScienceObject Oriented Programming

Constructor in JAVA.

By ICT Byte
2 Min Read
0

Last Updated on 5 years ago by ICT Byte

  • Constructor in JAVA: Introduction
  • What is the purpose of a constructor?
  • Syntax of Constructor
  • Advantage of constructors in Java
  • Characteristics / Properties of Constructor
  • Types of Constructors
    • Default Constructor
      • Syntax
      • Purpose of default constructor
    • Parameterized Constructor
      • Syntax
      • Syntax for calling parameterized Constructor
      • Purpose of Parameterized constructor
  • Overloaded Constructor
    • What is signature?
  • Object Parameterized Constructor

Constructor in JAVA: Introduction

  • special member method
  • it is called automatically (implicitly) when object is created
  • object is created to place own values, i.e not placing default values

Constructor is special member method which is called automatically when object is created.

What is the purpose of a constructor?

  • To initialize an object
  • This process in called initialization
  • Initializing is the process where user defined values are assigned at the time of memory allocation.

Syntax of Constructor

className()
{
…
…
…
…
}

Advantage of constructors in Java


If we write any Java program and we use concept of constructor, it

  • eliminates the palcement of default value
  • eliminates calling the normal method implicitly

Characteristics / Properties of Constructor

  • The name of the constructor must be the same as the name of the class
  • When an object is created, a constructor is called implicitly
  • The constructor should not return any value. This is true even return type is void. This means the constructor has no return type
  • There is no inheritance in the constructor
  • A constructor should not be static.
  • Constructors are not virtual
  • Constructors can be overloaded as they are member functions.

Types of Constructors

On the basis of creating an object, Java Constructors are divided into two.

  • Default/parameter less/zero argument/no argument constructor
  •   Parameterized constructor

Default Constructor

  • it never takes any parameters

Syntax


class NameofClass
{
…
classname()
{
//Statements;
}
…
}

Purpose of default constructor

  • to place same values in multiple objects of same class
  • provides default values to the objects
  • provides values depending on type
  • example for integer 0, for string null

Parameterized Constructor

  • takes parameter
  • contains list of variables in signatures

Syntax


class NameOfClass
{
NameofClass(list of parameters)
{
…
}
…..
}

Syntax for calling parameterized Constructor


new NameofClass(val1, val2, …)

Purpose of Parameterized constructor

  • create multiple objects of same class for different values

Overloaded Constructor

  • If the name of constructor is same but its signature are different, constructor is said to be overloaded.

What is signature?


Signature represents

  • number of parameters
  • parameter types
  • order of the parameter

Example
Test  t1= new Test(10, 20); –>1
Test  t2= new Test(100); –>2
Test  t3= new Test(10.5f, 20.5f); –>3
Test  t4= new Test(10.5f, 20); –>4
Test  t5= new Test(10, 20.5f); –>5
Here Test(—) is known as Overloaded Constructor.

Object Parameterized Constructor

  • it always takes object as a parameter
  • it aims to copy the content of object to another object (both objects should belong to same type)
  • this exactly resembles the copy constructor of C++

Example


class TestExample
       {
                int a,b;
                TestExample() // Default Constructor
                {
                                System.out.println("i am Default Constructor:");
                                x=1;
                                y=2;
                                System.out.println("The value of x="+x);
                                System.out.println("The value of y="+y);
                }

                TestExample(int a,int b)// Double parameterized constructor
                {
                                System.out.println("i am  Double Parameterized Constructor:");
                                x=a;
                                y=b;
                                System.out.println("The value of x="+x);
                                System.out.println("The value of by="+y);
                }

                TestExample(int a)// Single parameterized constructor
                {
                                System.out.println("i am  Single Parameterized Constructor:");
                                x=a;
                                y=a;
                                System.out.println("The value of x="+x);
                                System.out.println("The value of y="+y);
                }

                TestExample(TestExample a)// Object parameterized constructor
                {
                                System.out.println("i am  Object Parameterized Constructor:");
                                x=a.x;
                                y=a.y;
                                System.out.println("The value of x="+x);
                                System.out.println("The value of y="+y);
                }
       }

    class TestExample
       {
                public static void main(String[] args)
                {
                                TestExample t1= new TestExample();
                                TestExaple t2= new TestExample(10,20);
                                TestExample t3= new TestExample(100);
                                TestExample t4= new TestExample(t3);
                }
      }

Tags:

constructor in javaconstructor in oopobject oriented programming with javaoop
Author

ICT Byte

Follow Me
Other Articles
Previous

What is encapsulation? Learn encapsulation in JAVA

Samsung Galaxy A52 Price and Specifications
Next

Samsung Galaxy A52 Price and Specifications

Copyright 2026 — ICT BYTE. All rights reserved. Blogsy WordPress Theme