Everytime you create an object in Java one or several so-called constructors are called. You can say that they are used to initialize the object. It is the first thing that runs when the object is created and every class must have one. Well not entirely true, actually all the instance varables are assigned default values first.
A constructor looks a bit like a method but it isn't. It must not have a return type and it must have the same name as the class. It is invoked by the keyword "new". Here is a simple class with a constructor:
public class Wohill { public Wohill() { System.out.println("This is the constructor"); } public status void main(String[] args) { Wohill wo = new Wohill(); } } |
A new object of class 'Wohill' is created in the main function and on this creation the constructor is called. When running it prints
"This is the constructor".
It is also possible to leave out the constructor. And if you do, the compiler will create one for you (the default constructor). In this case it will be a no-arg constructor. If you however provide a constructor on your own, a no-arg or a var-arg constructor you are on you own. A class can hereby stated have one or several constructors. Having several is called having overloaded constructors. So you can implement constructors with the same name as above but with different argument lists. The reason for this is that there might be requirements to create an object with predefined values of different kinds or just a default one. However there can not be two constructors with the same argument list.
Below you can see several contructors implemented legally:
public class Wohill { // Instance variables private int width; private String name;
// A no-arg constructor public Wohill() { width=0; name="default"; }
// A var-arg constructor public Wohill(String aName, int aWidth) { name = aName; width = aWidth; }
// Another var-arg constructor public Wohill(int aWidth, String aName) { name = aName; width = aWidth; }
// And another var-arg constructor public Wohill(String name) { this.name = name; } } |
As mentioned shortly above several constructors can be called on an object creation. This is the case when using inheritance, i.e. if your class has a superclass. Class 'Wohill' below is the subclass of its superclass 'Blog'. Class 'Blog's superclass is class 'Object' which is the superclass of all classes.
public class Blog { public Blog() { System.out.println("Blog constructor"); } }
public class Wohill extends Blog { public Wohill() { System.out.println("Wohill constructor"); } }
|
When creating a Wohill object its constructor runs. The first thing that happens in the Wohill constructor is that its superclass constructor is called and so on. When we have reached the top constructor in the inheritance tree that constructor runs and after that the constructor that called it and so on until the Wohill constructor finally runs. This is called 'constructor chaining' and in this case it produces the output:
Blog constructor
Wohill constructorSome more facts:
- Constructors can use any access modifier.
- Interfaces do not have constructors
- A constructor can only be invoked from another constructor
- A superclass' constructor can be called explicitly user the keyword 'super' with or without arguments depending on the superclass constructor(s),
There are more information about this and again I would like to recommend the books "Head First Java" and "Sun Certified Programmer for Java 5 - Study Guide" by Kathy Sierra and Bert Bates from which I have been inspired and learned a lot about Java.