Wednesday, 30 March 2016

Constructors in java

Know About Constructors in java...........


Constructors in java:- a constructor is one of the special member method and it is automatically / implicitly called by JVM  during object creation time for placing our own values without placing default values.
Advantages of Constructors:-
When we develop the java application with the concept of constructors then such applications will get the following advantages-
·        Constructors eliminate default values.
·        Constructors eliminate in calling ordinary method.
In other word the purpose of constructor concept is to initialize the object.
Rules/Characteristic/properties:-
·        Constructor will be called implicitly/automatically during object creation time.
·        Each and every constructor name is similar to class name.
·        Constructor will not return any value even void also(if we write any return type then it will be treated as an ordinary method )
·        Constructor should not be static.(because whenever an object is created constructors will be called each & every time)
·        Constructors in java programming will not participate in inheritance process (because every constructor is mint for initializing its own class data members )
·        The access modifier of the constructor may or may not be private.
(a)  If the access modifier constructor is private an object of the corresponding class can be created in same class context of some other class.
(b) If the access modifier of the constructor is not private then we can create an object of corresponding class in the same class context and in another class context.

Types of constructors in java:-
 In java programming we have two types of constructors they are ….
·        Default/parameter less/zero argument constructor.
·        Para metered constructor.
1.     Default Constructor:- a constructor is said to be default if at only if it will not take any parameters.
The purpose of default constructor is to create multiple object with respect to same class for placing same values.
Syntax:-                       class <clsname>
                                      {
                                              <clsname>()
                                               {
                                                        Block of statements(s)………….initialization
                                                  }
                                             }
Ø Write a java program which illustrates the concept of default constructor.
  class Constructor
{
      int a,b;
      Constructor()
      {
                  System.out.println("Constructor...........DC");
                  a=1;
                  b=2;
                  System.out.println("the value of a="+a);
                  System.out.println("the value of b="+b);
      }
                 
}



class  ConstructorDemo
{
      public static void main(String k[])
      {
                  Constructor c1=new Constructor();
                  Constructor c2=new Constructor();
                  Constructor c3=new Constructor();

                 
      }
}


Note:- Whenever we create an object only with default constructor then defining the default construction is optional whenever we create an object with default constructor and if we are not defining default constructor then JVM will call its own constructor known as system defined default constructor & whose role is to place default values.

Rule1:- if we create an object with the default constructor and if we define the default constructor then JVM will execute programmer defined default constructor & places programmer defined values.