Constructor in Java





Constructor :

  • Java provides a special method called constructor .

  • A constructor initializes instance members.

  • It is special because its name same as the class.

  • Constructor doesn't have return type,even void.

  • Its invoked automatically when the object is created.

  • Constructor can be overload.

  • Constructors may have any accessibility, public, protected or private.


Types of Constructors :

Java has the following two types of constructor :
  1. Default Constructor : Constructor with zero parameter called default constructor.
  2. Parameterized Constructor : A constructor with one or more parameter called parameterized constructor.

Example :

//ConstructorDemo.java
class ConstructorDemo
{
//default constructor
 ConstructorDemo()
 {
  System.out.println("Default Constructor!!!");
 }

//Parameterized Constructor
 ConstructorDemo(int a)
 {
  System.out.println("Parameterized Constructor!!!");
 }
 public static void main(String []arg)
 {
  ConstructorDemo ob, ob1;

//default constructor     
  ob = new ConstructorDemo();

//Parameterized Constructor
  ob1 = new ConstructorDemo(10);
 }
}           
OUTPUT :
Default Constructor!!!
Parameterized Constructor!!!

prowessapps.in

If there is no explicit constructor defined in a class then compiler automatically creates a default constructor for that class. prowessapps.in

If we create any explicit constructor in a class then compiler would not create default constructor for that class. prowessapps.in

The main purpose of constructor is to initialize the fields (instance member or non-static variable of class) of the object with some dynamic calculated values.


Purpose of default Constructor :

Default constructor provides the default values to the object like 0, null etc. depending on the type.

Example :

class Employee
{
 int id;
 String name;
 void showDetails()
 {
  System.out.println("ID :"+id);
  System.out.println("NAME :"+name);
 }
 public static void main(String []arg)
 {
  Employee e = new Employee();
  e.showDetails();
 }
}            
OUTPUT :
ID :0
NAME :null   

Use of parameterized constructor:

Parameterized constructor is used to provide different values to the distinct objects.

Example :

In this example, we have created the constructor of Employee class that have two parameters. We can have any number of parameters in the constructor.
class Employee
{
 int id;
 String name;
 Employee(int n, String nm)
 {
  id = n;
  name = nm;
 }
 void showDetails()
 {
  System.out.println("ID :"+id);
  System.out.println("NAME :"+name);
 }
 public static void main(String []arg)
 {
  Employee e = new Employee(123,"Ayan");
  Employee e1 = new Employee(234,"Atif");
  e.showDetails();
  e1.showDetails();
 }
}            
OUTPUT :
ID :123
NAME :Ayan
ID :234
NAME :Atif 

Next topic is static




 





Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in



CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved