Wednesday, 14 February 2018

Constructor in Java

Constructor

Constructor in java is a special type of method that is used to initialize the object.In Java, constructor is a block of codes similar to method. It is called when an instance of object is created and memory is allocated for the object. Java constructor is invoked at the time of object creation. It constructs the values provides data for the object that is why it is known as constructor.  You cannot write two constructors that have the same number and type of arguments for the same class.

Syntax Java Constructors:-

The new keyword creates the object of class Myname and invokes the constructor to initialize this newly created object.

Rules for creating java constructor

There are basically two rules defined for the constructor.
  1. Constructor name must be same as its class name
  2. Constructor must have no explicit return type

Types of java constructors:-

  1. Default constructor (no-arg constructor)
  2. Parameterized constructor

Java Default Constructor:-

A constructor is called "Default Constructor" when it doesn't have any parameter. Java compiler will automatically create a no-argument constructor during run-time. This constructor is known as default constructor. The default constructor initializes any uninitialized instance variables.

Syntax of default constructor:-

<class_name>(){}
Default Constructor Example:-

public class EX_Costructor {

public EX_Costructor() {
// null or non parameterized constructor;
}

public static void main(String[] args) {

EX_Costructor object = new EX_Costructor();
object.hello();

}

public void hello() {
System.out.println("Hello Constructor");
}


}

Output:-
Hello Constructor

Explanation: In the above class, you are not creating any constructor so compiler provides you a default constructor. Here 0 and null values are provided by default constructor because 0 value show integer and Null value show String.

Java parameterized constructor:-

A constructor which has a specific number of parameters is called parameterized constructor.
Parameterized constructor is used to provide different values to the distinct objects.
ClassName (arg1,arg2, arg3,………argn){
// constructor body
}
  1. Constructor Can Take Value , Value is Called as – Argument.
  2. Argument can be of any type i.e Integer, Character, Array or any Object.
  3. Constructor can take any number of Argument

Constructor Overloading:-

Constructor overloading in Java is a technique of having more than one constructor with different parameter lists. They are arranged in a way that each constructor performs a different task. They are differentiated by the compiler by the number of parameters in the list and their types.Constructor overloading is a concept of having more than one constructor with different parameters list, in such a way so that each constructor performs a different task. In Java, a constructor is just like a method but without return type. It can also be overloaded like Java methods.

Important notes:-

  • Constructors are invoked implicitly when you instantiate objects.
  • The two rules for creating a constructor are:
    • A Java constructor name must exactly match with the class name.
    • A Java constructor must not have a return type.
  • If a class doesn't have a constructor, Java compiler automatically creates a default constructor during run-time. The default constructor initialize instance variables with default values. For example: int variable will be initialized to 0
  • Constructor types:
    • No-Arg Constructor - a constructor that does not accept any arguments
    • Default Constructor - a constructor that is automatically created by the Java compiler if it is not explicitly defined.
    • Parameterized constructor - used to specify specific values of variables in object
  • Constructors cannot be abstract or static or final.
  • Constructor can be overloaded but can not be overridden.

0 comments:

Post a Comment