X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Constructor in Java :

A constructor is a special member method which will be called by the JVM implicitly (automatically) for placing user/programmer defined values instead of placing default values. Constructors are meant for initializing the newly created object.


Advantages of Constructor :

  • A constructor eliminates placing the default values.

  • A constructor eliminates calling the normal method implicitly.

Rules / Properties / Characteristics of a Constructor :

  • Constructor name must be similar to the name of the class.

  • Constructor should not return any value even void also (if we write the return type for the constructor then we won't get any compile time or runtime errors because that constructor will be treated as ordinary method).

class Algo
{
 void Algo() // it is a normal method but not a constructor
 {
 
 }
}
  • Constructors should not be static since constructors will be called each and every time whenever an object is creating.

  • Constructor should not be private provided an object of one class is created in another class (constructor can be private provided an object of one class created in the same class). Hence, the only applicable modifiers for constructors are public, private, protected and <default>. If we are trying to use any other modifier then we will get compile time error saying "modifier xxxx not allowed here".

class Algo
{
 final Algo()
 {
 
 }
} // Compile time error : modifier final not allowed here
  • Inheritance and overriding concepts are not applicable for Constructors.

  • Every class in Java including abstract class can contain Constructor. But interface can't have Constructors.

  • Constructors are called automatically whenever an object is creating.

  • Recursive method call is always runtime exception whereas recursive constructor invocation is a compile time error.

X

invocation :

It refers to the action of invoking (call on) someone or something.

class Algo
{
 public static void m1()
 {
   System.out.println("Algo");
   m2();
 }
 public static void m2()
 {
   System.out.println("Valley");
   m1();
 }
 public static void main(String [ ] args)
 {
   m1();
 }
} // Runtime Exception : StackOverflowError
class Algo
{
 Algo()
 {
   this(10);
   System.out.println("Algo");
 }
 Algo(int a)
 { 
   this();
   System.out.println("Valley");
 }
 public static void main(String [ ] args)
 {
  
 }
} // Compile time error : recursive constructor invocation
  • If base class constructor throws some checked exception then it is compulsory that the derived class should throw some checked exception otherwise we will get compile time error.

import java.io.*;
class Base
{
 Base() throws IOException
 {
 
 }
}
class Derived extends Base
{

} // Compile time error : unreported exception IOException; must be caught or declared to be thrown
import java.io.*;
class Base
{
 Base() throws IOException
 {
 
 }
}
class Derived extends Base
{
 Derived() throws Exception
 {

 }
} // valid

Types of constructors :

Based on creating objects in Java we have two types of constructors. They are :

  • Default / parameter less / no argument constructor




  • Parameterized constructor




  • Default / parameter less / no argument constructor :


  • A default constructor is one which will not take any parameter.

  • The access modifier of default constructor is same as class modifier but this is applicable for public and <default> modifiers.

Syntax : class <class_name> { class_name() // default constructor { Block of statements; } }

Example :

class Algo { int a, b; Algo() { System.out.println("Algo Constructor"); a = 10; b = 20; System.out.println("Value of a = " + a); System.out.println("Value of b = " + b); } } class Example { public static void main(String [ ] args) { Algo a = new Algo(); } }

Output :

Algo Constructor Value of a = 10 Value of b = 20
  • Whenever we create an object only with default constructor, defining the default constructor is optional. If we are not defining default constructor of a class then JVM will call automatically system defined default constructor. If we define it then JVM will call user/programmer defined default constructor.

  • Hence, a class can contain either programmer written constructor or compiler generated constructor but not both simultaneously.

Parameterized constructor :


  • A parameterized constructor is one which takes some parameters.

Syntax : class <class_name> { <class_name>(list of parameters) // parameterized constructor { Block of statements; } }

Example :

class Algo { int a, b; Algo(int c, int d) { System.out.println("Algo Constructor"); a = c; b = d; System.out.println("Value of a = " + a); System.out.println("Value of b = " + b); } } class Example { public static void main (String [ ] args) { Algo a = new Algo(10, 20); } }

Output :

Algo Constructor Value of a = 10 Value of b = 20
  • Whenever we create an object using parameterized constructor, it is mandatory for the Java programmer to define parameterized constructor otherwise we will get compile time error.


X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.