Aug 26, 2011


       A java constructor has the same name as the name of the class to which it belongs. Constructor’s syntax does not include a return type, since constructors never return a value.
Constructors may include parameters of various types. When the constructor is invoked using the new operator, the types must match those that are specified in the constructor definition.
Java provides a default constructor which takes no arguments and performs no special actions or initializations, when no explicit constructors are provided.
The only action taken by the implicit default constructor is to call the superclass constructor using the super() call. Constructor arguments provide you with a way to provide parameters for the initialization of an object.
      Below is an example of a DrawingEx class containing 2 constructors. (one default and one parameterized constructor).
                   public class DrawingEx {

                   int length;
                   int breadth;
                   int height;
                   public int getVolume() {
                   return (length * breadth * height);
                   DrawingEx () {
                   length = 10;
                   breadth = 10;
                   height = 10;
                   DrawingEx (int l, int b, int h) {
                   length = l;
                   breadth = b;
                   height = h;
                  public static void main(String[] args) {
                  DrawingEx obj1, obj2;
                  obj1 = new DrawingEx ();
                  obj2 = new DrawingEx (10, 20, 30);

                  System.out.println("Volume of First cube is : " + obj1.getVolume());
                  System.out.println("Volume of  second Cube is : " + obj2.getVolume());

If a class defines an explicit constructor, it no longer has a default constructor to set the state of the objects. If such a class requires a default constructor, its implementation must be provided. Any attempt to call the default constructor will be a compile time error if an explicit default constructor is not provided in such a case.
Constructor overloading
       Constructors can be overloaded just as regular methods. Here is an example of a class with an overloaded constructor:
                   public class date
                    int year;
                    int month;
                    int day;
                    date(int year)
                      this.year = year;
                      month = 1;
                      day = 1;
                    date(int year/int month,int day)
                       this.year = year;
                       this.month = month;
              = day;
You could create an object of that class with statements like either of the following:
                    date a = new date(2010);
                    date b = new date(2010,5,15);
Naturally, the first statement will call the first constructor, because it has just one parameter. The second statement has three parameters, so it will call the second constructor.
If a class has at least one constructor, then a constructor will always be called when an object is created. This means you must always supply the appropriate parameters for at least one of the constructors.

Rules for Constructor ***
  •  Constructors can use any access modifier, including private. (A private constructor means only code within the class itself can instantiate an object of that type, so if the private constructor class wants to allow an instance of the class to be used, the class must provide a static method or variable that allows access to an instance created from within the class.)
  • The constructor name must match the name of the class.
  • Constructors must not have a return type.
  • It’s legal (but stupid) to have a method with the same name as the class, but that doesn’t make it a constructor. If you see a return type, it’s a method rather than a constructor. In fact, you could have both a method and a constructor with the same namethe name of the classin the same class, and that’s not a problem for Java. Be careful not to mistake a method for a constructor, be sure to look for a return type.
  • If you don’t type a constructor into your class code, a default constructor will be automatically generated by the compiler.
  • The default constructor is ALWAYS a no-arg constructor.
  • If you want a no-arg constructor and you’ve typed any other constructor(s) into your class code, the compiler won’t provide the no-arg constructor for you. In other words, if you’ve typed in a constructor with arguments, you won’t have a no-arg constructor unless you type it in yourself !
  • Every constructor has, as its first statement, either a call to an overloaded constructor (this()) or a call to the superclass constructor (super()), although remember that this call can be inserted by the compiler.
  • If you do type in a constructor (as opposed to relying on the compiler-generated default constructor), and you do not type in the call to super() or a call to this(), the compiler will insert a no-arg call to super() for you, as the very first statement in the constructor.
  • A call to super() can be either a no-arg call or can include arguments passed to the super constructor.
  • A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor, although the default constructor is always a no-arg constructor. The default constructor is the one the compiler provides! While the default constructor is always a no-arg constructor, you’re free to put in your own noarg constructor.
  • You cannot make a call to an instance method, or access an instance variable, until after the super constructor runs.
  • Only static variables and methods can be accessed as part of the call to super() or this(). (Example: super(Animal.NAME) is OK, because NAME is declared as a static variable.)
  • Abstract classes have constructors, and those constructors are always called when a concrete subclass is instantiated.
  • Interfaces do not have constructors. Interfaces are not part of an object’s inheritance tree.
  • The only way a constructor can be invoked is from within another constructor.


Post a Comment