Sunday 23 October 2016

constructor

constructor
  • Constructor is used to initialize the instance variables
  • At the time of object creation if corresponding constructor exists for that object in class then it will execute automatically.
Rules for constructor
  • Constructor name and class name must be same
  • Constructor does not return any value even void also
Types of constructor : 2 Types
  1. Default constructor
  2. parameterized constructor
Default constructor : 
  • A constructor which has no parameter is called default constructor
Syntax

            class classname
            {
                   classname()
                   {
                           body of default constructor;
                   }
            }
--------------------------------------------------------------------------
Program        :       Defualt constructor program
Program name :       DefConDemo1.java
Output           :        
                            Default constructor
                               
class DefConDemo1
{
        DefConDemo1()
        {
                 System.out.println("Default constructor");
        }

        public static void main(String args[])
        {
                DefConDemo1 d = new DefConDemo1();    
        }
}

Compile          :      javac DefConDemo1.java
Run                :      java DefConDemo1
Output            :        
                            Default constructor
--------------------------------------------------------------------------
Note
  • If class have no default constructor then compiler will create the default constructor automatically.
  • Default constructor will provide the defalut values to object.
--------------------------------------------------------------------------
Program        :       Defualt constructor program
Program name :      Company.java
Output           :        
                            Id is : 0
    Name is : null   
class Company
{
        int id ;
        String name ;

        Company()
        {
                 System.out.println("Id is :"+id);
                 System.out.println("Name is :"+name);
        }

        public static void main(String args[])
        {
                Company c = new Company ();    
        }
}

Compile          :      javac Company.java
Run                :      java Company
Output            :        
                            Id is : 0
    Name is : null
--------------------------------------------------------------------------
Parameterized constructor
  • A constructor that has parameter is known as parameterized constructor.
Why parameterized constructor?
  • Parameterized constructor is used to provide different values to the distinct
    objects.
Syntax
            class classname
            {
                   classname(list of parameters)
                   {
                           body of parameterized constructor;
                   }
            }
 --------------------------------------------------------------------------
Program        :       Parameterized constructor program
Program name :      Student.java
Output           :        
                            Id is : 1
    Name is : Mohan

                            Id is : 2
                            Name = Nireekshan
   
class Student
{
        int id ;
        String name ;

        Student(int i, String n)
        {
                id =i;
                name = n;

                System.out.println("Id is :"+id);
                System.out.println("Name is :"+name);
        }

        public static void main(String args[])
        {
                Student s1 = new Student(1, “Mohan”);   
                Student s2 = new Student(2, “Nireekshan”);   
        }
}

Compile          :      javac Student.java
Run                :      java Student
Output            :        
                             Id is : 1
     Name is : Mohan

                             Id is : 2
                             Name = Nireekshan
 --------------------------------------------------------------------------
Thanks for your time.
Nireekshan