Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is solution for this??

 
suman bingi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
suman bingi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
thank you sivalingam.
suman.
 
malathi latha
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
can any one explain how 'this' shortens the code above.
Thank you.
 
Golam Newaz
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic