posted 23 years ago
Hi Malathi,
Before going into your question, we have to explore about constructor.
Sometimes a class contains many constructors and each constructor allows the caller to provide initial values for different instance variables of the new object. For example, java.awt.Rectangle has these three constructors:
Rectangle() {
this(0,0,0,0);
}
Rectangle(int width, int height) {
this(0,0,width,height);
}
Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
The no-argument constructor doesn't let the caller provide
initial values for anything, and the other two constructors let the caller set initial values either for the size or for the origin and size. Yet, all of the instance variables, the origin and the size, for Rectangle must be initialized. In this case, classes often have one constructor that does all of the work.
The important is that The other constructors call 'this' constructor and provide it either with the values from their parameters or with default values. For example, here are the possible implementations of the three Rectangle constructors shown previously (assume x, y, width, and height are the names of the instance variables to be initialized):
Now we can come to question in how 'do everything same as a single argument. So if you call a Constructor like
Example a=new Example( 10, 12);
Look which Constructor we are calling definitely second one
as per Sivalingan's modified program. Here you are calling second
constructor and inside that Constructor you are calling
this(a); that means you are calling 1st constructor( following one ) which we can compare with this(a). So at the same you are
doing everything using one Constructor arguments.
public Example( int a){
x=a;
}
Understood it,
- Golam Newaz