File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is solution for this?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what is solution for this??" Watch "what is solution for this??" New topic
Author

what is solution for this??

suman bingi
Greenhorn

Joined: Nov 19, 2000
Posts: 21
hello,
this is a qeustion from javacross
Q].public class Example {
int x, y;
public Example( int a) {
x=a;
}
public Example( int a, int b) {
y=b;
}
}
What is the most concise way to code the �do everything the same as single argument?
Thank you
Sivalingam Sivasuthan
Ranch Hand

Joined: Jan 12, 2001
Posts: 75
Hi suman bingi:
public class Example {
int x, y;
public Example( int a) {
x=a;
}
public Example( int a, int b) {
this(a);
y=b;
}
}
Siva

- Siva<br /> Sun Certified Programmer for the Java™2 Platform
suman bingi
Greenhorn

Joined: Nov 19, 2000
Posts: 21
Hi ,
thank you sivalingam.
suman.
malathi latha
Greenhorn

Joined: Feb 11, 2001
Posts: 21
Hi,
can any one explain how 'this' shortens the code above.
Thank you.
Golam Newaz
Ranch Hand

Joined: Jan 08, 2001
Posts: 64
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is solution for this??