JaiswalTraining
Get the online training

Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com





Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven








CONSTRUCTOR
  • Constructor is used for initializing  instance variable.
  • Constructor name must be  same as  of class name.
  • Constructors are automatically called, when the instance is created.
  • On not defining constructor compiler  creates a default constructor.
  • Constrcutor does not return any value. 
  • If constroctor  then it is the method.
  • A subclass does not inherit the constructor from the superclass.

  class without constructor
Example 1:
  • In this example, we are creting a class with  two instance variable.  
  • There is no constrcutor defined.
  • Instance variable have null value because they are not initialized.
  • If  we want to initialize instance variable,  use the constructor.
public class stud {
  int roll;
  String name;

  public static void main(String[] args) {
    stud s = new stud();
    System.out.println("roll is " + s.roll);
    System.out.println("name  is " + s.name);
  }
}

Output:

javac stud.java

java stud
roll is 0
name  is null

Download this code

class without constructor
Example 2:
  • In this example we have created a class and two instance variable same as above.
  • But here we  create constructor for initializing  instance variable.
  • So we  initialize the instance variable with the help of constructor

public class stud1 {
  int roll;
  String name;

  stud1() {
    roll = 1;
    name = "sohan";
  }

  public static void main(String[] args) {
    stud1 s = new stud1();
    System.out.println("roll is " + s.roll);
    System.out.println("name  is " + s.name);

  }
}

Output:

javac stud1.java
java stud1
roll is 1
name  is sohan

Download this code

Benifit of the constructor.
  • It is called when object is created.
  • It is used to initialize the class variable.

Every time constrcutor is called when object is created

Example 3:
  • In this example two objects are  created.
  • So two times constructor for two differrent objects are called
  • This way differrent objects are having differrent copy of the instance variable.

public class stud2 {
  int roll;
  String name;

  stud2() {
    roll = 1;
    name = "sohan";
  }

  public static void main(String[] args) {
    stud2 s = new stud2();
    stud2 s1 = new stud2();

    System.out.println("roll is " + s.roll);
    System.out.println("name  is " + s.name);
    
    System.out.println("roll is " + s1.roll);
    System.out.println("name  is " + s1.name);

  }
}


javac stud2.java
java stud2
roll is 1
name  is sohan
roll is 1
name  is sohan

  • Here both objects have same value
  • So default constructor is useful for one object creation.
Download this code

Overloaded  Constructor
Example 4:
  • Overloaded constructor initialize instance variables with differrent  values.
  • With different object different values are passed.
  • So we can  retrive different value for  instance variable with differrent objects.

public class stud3 {

  int roll;
  String name;

  stud3(int roll, String name) {
    this.roll = roll;
    this.name = name;
  }

  public static void main(String[] args) {

    stud3 s = new stud3(1"mohan");
    stud3 s1 = new stud3(2"sohan");

    System.out.println("roll is " + s.roll);
    System.out.println("name  is " + s.name);

    System.out.println("roll is " + s1.roll);
    System.out.println("name  is " + s1.name);
  }
}

Output:

javac stud3.java
java stud3
roll is 1
name  is mohan
roll is 2
name  is sohan

Download this code

Constructor does not return any value
  • Constructor does not return any value.
  • If there is constrcuctor which returns value then it is the method.
  • Methods can have the same name as that of the class.


Example 5:
  • Here we have  not created a constructor.
  • So instance variable is not initialized.
  • mystud() has  a return type so it is not the constructor.
  • Now this is the method.

public class mystud {

  int roll;
  String name;

 void mystud() {
    roll = 1;
    name = "sohan";
  }

  public static void main(String[] args) {
    mystud s = new mystud();
    System.out.println("roll is " + s.roll);
    System.out.println("name  is " + s.name);

  }
}

Output:

javac mystud.java
java mystud
roll is 0
name  is null

Download this code


Example 6:
  • In this example, there is no constructor.
  • Inside the method instance variable is initialized.
  • Method initialized value are limited to method scope.

public class mystud1 {

  int roll;
  String name;

  void mystud() {
    roll = 1;
    name = "sohan";
  }

  public static void main(String[] args) {
    mystud1 s = new mystud1();
    System.out.println("roll is " + s.roll);
    System.out.println("name  is " + s.name);
    
    s.mystud();
    System.out.println("roll is " + s.roll);
    System.out.println("name  is " + s.name);

  }
}


javac mystud1.java
java mystud1
roll is 0
name  is null
roll is 1
name  is sohan

  • The first output is   0 and null .
  • These are default values for the instance variable.
  • Now method is called which initialize the instance variables.
  • So it has initialized  value of the method.
Download this code

Example 7:
  • In this example constructor and method both have same name.
  • In mehod and constructor instance varibles are initialized differrently.
  • Method have local copy.

public class mystud2 {

  int roll;
  String name;

  void mystud2() {
    roll = 1;
    name = "sohan";
  }
  
  
  mystud2() {
      roll = 2;
      name = "rohan";
    }

  public static void main(String[] args) {
    mystud2 s = new mystud2();
    System.out.println("roll is " + s.roll);
    System.out.println("name  is " + s.name);
    
    s.mystud2();
    System.out.println("roll is " + s.roll);
    System.out.println("name  is " + s.name);

  }
}

Output:

javac mystud2.java
java mystud2
roll is 2
name  is rohan
roll is 1
name  is sohan

Download this code


Constructor with in the package

  • Constructor can be created inside the package.
  • But it has to follow the access modifier.
  • Constrcutor can have following access modifier
    • public
    • protected
    • default
    • private
Default Constructor (default access modifier)
  • In this case we will not use any access modifier with the constructor.
  • So this is the case of default

    Constructor in differrent package

  • If there is class  in differrent package.
  • And the class is having default constructor
  • Then it cannot be used outside the class


Example 8:
  • In this example, we has created package org in a different folder com.
  • Then create a class welcome in org package and the class has default constructor.
  • After that we create class hello outside the  package
  • And now create object of welcome class in hello
  • There is some compilation problem.
  • This means we cannot  use  any constructor and function outside of package
    if they are default.

package com.org;

public class welcome {

welcome(){}

}

import com.org.*;

public class hello {

public static void main(String[] args) {
  welcome w=new welcome();
}
}

Output:

javac hello.java
hello.java:6: welcome() is not public in com.org.welcome; cannot be accessed fro
m outside package
        welcome w=new welcome();
                  ^
1 error

     Constructor in same package
  • If there is class  in same package.
  • And the class is having default constructor
  • Then it can be used by other class of the same package

  • In this example, we are creating class jack in same package as well as welcome class.
  • There is no compilation problem, because both class are in same package.
package com.org;

public class jack {
public static void main(String[] args) {
  welcome w=new welcome();

}
}


C:\code>javac com\org\jack.java


Download this code

public constructor

  • Now we will use  public access modifier with constructor.
  • So its object can be created in the
    • same package,
    • outside the package also
  • But class should be public

Example 9:
  • In this example, welcome1 class constructor is public.
  • So it must be access by any outside/inside class.

package com.org;

public class welcome1 {

public welcome1(){
  System.out.println("Welcome file");
}

}


import com.org.*;

public class hello1 {

public static void main(String[] args) {
  welcome1 w=new welcome1();
}
}

Output:  

javac hello1.java
java hello1
Welcome file

Download this code


protected constructor

  • Constructor can be protected.
  • For this u need to use the protected keyword.
  • In this case the class object cannot  be created outside the package.
  • Class object can be created only by inheritance.
  • The class should be extended.
  • Then its object can be created.
Example 10:
  • In this example, constructor of welcome2 class is protected.
  • Protected member function cann't be access different class member.

package com.org;

public class welcome2 {

protected welcome2(){}

}

import com.org.*;

public class hello2 {

public static void main(String[] args) {
  welcome2 w=new welcome2();
}
}



C:\code>javac hello2.java
hello2.java:6: welcome2() has protected access in com.org.welcome2
        welcome2 w=new welcome2();
                   ^
1 error



  • Now we will create another class hello5
  • In this case we will extends the welcome2.
  • Now it will get compiled

import com.org.*;

public class hello5 extends welcome2 {

public static void main(String[] args) {
  welcome2 w=new welcome2();
}
}


C:\code>javac hello5.java
                


Download this code

private constructor

  • Constructor can be private
  • private constrcutor cannot be used ouside the class

Example 11:
  • In this example, we are creating a private constructor of welcome3 and
    creating object of welcome3 class in hello3.
  • But there is a problem in compilation because we cann't access private member
    function or variable from inside or outside package. 

package com.org;

public class welcome3 {

private welcome3(){}

public static void main(String[] args) {
  welcome3 w=new welcome3();
}
}


import com.org.*;

public class hello3 extends welcome3 {

public static void main(String[] args) {
  welcome3 w=new welcome3();
}
}

Output:

C:\code>javac hello3.java
hello3.java:3: welcome3() has private access in com.org.welcome3
public class hello3 extends welcome3 {
       ^
hello3.java:6: welcome3() has private access in com.org.welcome3
        welcome3 w=new welcome3();
                   ^
2 errors


package com.org;
public class jack1 {
  public static void main(String[] args) {
    welcome3 w = new welcome3();
  }
}


javac com\org\jack1.java
com\org\jack1.java:5: welcome3() has private access in com.org.welcome3
        welcome3 w=new welcome3();
                   ^
1 error

Download this code