• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what is solution for this??

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
thank you sivalingam.
suman.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
can any one explain how 'this' shortens the code above.
Thank you.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic