Wednesday 6 November 2013

Session 8:- Constructor

      Constructor
  • Constructor is one which is made for constructing or crating object and to initialize the  properties (States) when we create an object.
  •  Constructor name is must be same as class name.
  •  Constructor is not made for any returning the value .It can't return so constructor has not any  return type.
  • The constructor must be present is same appropriate class.
  •  When we create a class than automatically constructor created, that is called default constructor.
  • The default constructor will be public and zero parametrized.
  •  In any class we can write our own constructor which can be parameter or zero parametrized and which is called as user defined or custom constructor.
  •  When we write our own constructor than default constructor will be automatically destroyed.
  • The purpose of our own constructor is to initialize properties or states of object at the time of object creation.

Example :-Constructor
            public class Book{
               String title;
               int price;
               public Book()   
                   {   
              // Default Constructor, it is not visible !!!.
              // Instance variable Assign default value
                     }
          public Book(String name , int p)
                {

               // custom Constructor
                   title=name;
                    price=p;
              }
                         }
          public class Run{
          public static void main(String args[]){
            Book b=new Book();
            Book b1=new Book(“inside java”,100000);
                }
               }    
          NOTE
  • The local variable & instance variable name can be same at that time local scope variable dominate over the instance variable we can use “thiskeyword to differentiate local and instance variable.
  •  When we use “this” with a variable than it’s refer to instance variable.
  • Constructor can return the value:-
  1. java perspective constructor can't Return the value but JVM perspective constructor can return the value because the object create by the jvm and that time constructor call and you store the object in some reference variable so constructor can return the value and that value is reference or address of created object.
Example :-

     public class Car     
    {       
     String name;   
     long  price;   
     public Car(String name, int price) 
          {                    
         this.name=name;
     // instance variable = local variable                             this.price=price;         
          System.out.println (this.price);

          System.out.println (this.name);

         }     
       } 
    public class Run{   
    Public static void main (String args[]) 
    {     
    Car c=new Car(“Audi”,10000000);
          }
        }
OUTPUT:-
              Audi
              10000000

Example :-

public class BabyGirl {
     String clr;
     String hclr;
     long price;
     String eyeclr;
    public BabyGirl()
    {    
    clr="white";
    hclr="black";
    eyeclr="brown";
    System.out.println (“Color:-”+clr);
    System.out.println (“Hair color:-”+hclr);
    System.out.println (“Eye color:”+eyeclr); 
     }
      public class Run {
 public static void main(String[] args) {
    new BabyGirl();
     }
           } 
        OUTPUT:-
Color:-white
Hair color:-black
Eye color:-brown

No comments:

Post a Comment