This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why is it wrong to use this() in an overloading method?

 
Woo Hwang
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I complied this code below, an error occured because of use of this() in an overloading method, and compiler also said that only contructors could invoke contructors. What does that mean?
public class test {
private int x,y;
private float z;

public void setVar(int a,int b,float c){
x=a;
y=b;
z=c;
}

public void setVar(int a,float c, int b){
this(a,c,b);
}
}

 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Woo,
The keyword 'this' refers to the current class. The JVM reserves the right of 'this' to be used in only one place as a method call:
The very first line of any constructor. In that position it has only one purpose: to call an overloaded constructor. Therefore, this() calls the class constructor matching the signature of parameters given.
It can't be used to call a constructor from any other method because that would not make programmitical sense. If any other method wants an object of the class type then just call the constructor explicitly!
The original intent of the this() method call was to allow a programmer to have all the initialization work in one routine and have various API (constructors) to declare the class type without having to duplicate all the work in each routine.
To call your overloaded method you can just call it using its name. Your example has one major flaw in it though, because if this() method call did work you would just be calling yourself and not the overloaded method. To call the other method, you would need to reposition the parameters:
public void setVar( int a, float c, int b )
{
setVar( a, c, b ); // Calls this method again!?!
setVar( a, b, c ); // Call overloaded method.
}
Regards,
Manfred.
 
Chad McGowan
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Woo Hwang:
When I complied this code below, an error occured because of use of this() in an overloading method, and compiler also said that only contructors could invoke contructors. What does that mean?
public class test {
private int x,y;
private float z;

public void setVar(int a,int b,float c){
x=a;
y=b;
z=c;
}

public void setVar(int a,float c, int b){
this(a,c,b);
}
}

I think the compiler gives a pretty good explanation. this(a,b,c) is a call to the constructor test(int a, float b, int c), (which doesn't exist anyway). You can only invoke this call from within another constructor. Think about a constructor as being an initialization of an object that happens one time. You can't do this from a method belonging to the object. The reason being, if you already have an object that you use to call the method, how can that method construct an object?
Chad
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you still have withdrawal symptoms from not seeing "this" somewhere in the method call, do this (no pun intended) instead:

Pho
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic