Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Saturday, 21 January 2017

Constructor in Java

 

Constructor

Constructor in java is a special type of method or member function of a class which is used to initialize the objects, object initialization means putting the values into data member. Constructor are required to create an objects for a class and Java constructor is invoked at the time of object creation.

When the object is created , constructor executes first. Any code you have in your constructor will then get executed. You don't need to make any special calls to a constructor , they happens automatically when you create new object.


Rules for creating constructor in java 

  • Constructor name must be same as it class name
  • Constructor must have no explicit return type


Types of java constructor

  • Default constructor (non-arg constructor)
  • Parameterized constructor 
Constructor in Java


Default Constructor in Java

Default constructor is a constructor that have no arguments or parameters.


Example of Default Constructor in Java

class Students
{
Students()//creating default constructor
{
System.out.println("default constructor is running");
}
public static void main(String args[])
{
Students d = new Students();//creating object with new keyword
}
}

output will be : default constructor is running


👉If we don't explicitly declare a constructor for any java class the compiler builds a default constructor for that class.


For example:


                                                                                             

class Students                                                                  class Students 
{                                                                      →                        
public static void main(String args[])      compiler               Students()
{                                                                                                    {
Students s = new Students();                                                     
 }                                                                                                       }
}                                                                                 p s v m(String args[])
                                                                                                   
                                                                                                               }}


What is the use of default constructor?

Default constructor provide the default values to the objects like 0 and null. Depending on the data types.

Default constructor example that provides default values 

class Student
{
int rollno;
String name;
void display()
{
System.out.println(rollno);
System.out.println(name);
}
public static void main(String...s)
{
Student ss = new Student();//creating object of Student class
ss.void();//calling method
}
}

output: 0

              null

Here in the above class we are not creating any constructor so compiler provides you default constructor. 0 and null provided by default constructor.



Parameterized Constructor in Java

A constructor that have parameters is known as parameterized constructor.


Why use parameterized constructor in java?

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

Example of parameterized constructor

In this example, we have created constructor of Employees class that have two parameters. We have any number of parameters in the constructor.

class Employees
{
int id;
String name;
Employees(int i, String n)// two parameter
{
id = i;
name = n;
}
void show()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
Employees e = new Employees(1,"prem");
Employees ee = new Employees(2,"ram");
e.show();
ee.show();
}
}

output: 1 prem
              2 ram 


Another example of parameterized constructor


class Employees
{
int id;
int age;
Employees(int i, int a)
{
System.out.println(" i am parameterized constructor");
id = i;
age = a;
System.out.println("id is : " +id);
System.out.println(" age is : " +age);
}
public static void main(String args[])
{
Employees e1 = new Employees(100,26);
}
}

output: i am parameterized constructor
              id is : 100
              age is : 26


Some point related to parameterized constructor

  1. Whenever we create an object using parameterized constructor, it must be defined parameterized constructor otherwise we will compile time error.
  2. Whenever we define the objects with respect to both parameterized constructor and default constructor, It must be define both the constructor.
  3. You can keep maximum one default constructor and 'n' number of parameterized constructor in class.
Share:

1 comment:

  1. I like this Java constructor article, thanks

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate