Thursday, 4 February 2016

Constructor

By

Constructors are the special method that have the same name as that of the class
          class Demo{
            public Demo(); // Constructor
          }

Constructors are used to initialize the instance variable of class so called constructors as the constructs the instance variable inside heap.
There are two types of constructors
  • Default constructors
  • Parameterized constructors
Default Constructors
Whenever there is no constructor given in the class default constructor is provided by compiler to class.
Default constructors do not have parameters or arguments and thus initialize instance variable to default values.
User defined values to the instance variable can be given inside the constructor.

There is a hard rule i.e : Every class have constructor.
class Customer{
  private int customerId;
  public int getCustomerId(){
  return customerId;
  }
}
class Retail{
public static void main(String args[]){
  Customer custObj=new Customer(); // System provides a Default constructor
  System.out.println("Customer Id:"+
  custObj.getCustomerId());
}
}

Output: Customer Id:0  because we have't initialize the customer id inside the default constructor and instance variable is initialized to default value 0 because int by default value is 0.

What should be do to get Output as "Customer Id =100"  

class Customer{
  private int customerId;
  public Customer(){ // line 1
 customerId = 100;   // line 2
  }
  public int getCustomerId(){
  return customerId;
  }
}

Line 1 and 2 are the definition of default constructor where we initialize our instance variable
and rest of the code is same now 
Output:Customer Id:100


Parameterized Constructors

Parameterized constructor  are having the same name as that of class as default constructor does.
The only difference is that parameterized constructor can have parameters or arguments which are  user defined values passed to it so as to initialize instance variable with that passed values

class Customer{
  private int customerId;
  public Customer(int id){ // Parameterize constructor
 customerId = id;  
  }
 public int getCustomerId(){
  return customerId;
  }
}
class Retail{
public static void main(String args[]){
  Customer custObj=new Customer(100); // Parameterized constructor   and passed value
  System.out.println("Customer Id:"+
  custObj.getCustomerId());
}
}

Output:Customer Id:100

Let take case study where we want to assign a customer id  starting from 1 to every time a customer comes i.e: for first customer customer Id will 1 and for second it will be 2 and so on..

class Customer{
  private int customerId = 1;
  public Customer(){
  customerId++;
  }
  public int getCustomerId(){
  return customerId;
  }
}
class Retail{
  public static void main(String args[]){
  Customer custObj1= new Customer();
  System.out.println("Customer Id:" + custObj1.getCustomerId());
  Customer custObj2= new Customer();
  System.out.println("Customer Id:" + custObj2.getCustomerId());
  }
}

Output: Customer id: 2
              Customer id: 2
For every customer Customer id will be same i.e 2 why?
Reason is customerId is an instance variable and it will be created separately for each  object so every time the object is created constructor is called and initialize the customerId with 1 and inside constructor it is incremented by 1 so customerId will be 2 for all.
Hence we need a variable common to whole class. and that is Static variables this will be discussed in next post.
If you have any query you can comment.
Thanks for reading my posts hope they are helping  you

0 comments :

Post a Comment