File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Why is it wrong to use this() in an overloading method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why is it wrong to use this() in an overloading method?" Watch "Why is it wrong to use this() in an overloading method?" New topic
Author

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

Woo Hwang
Greenhorn

Joined: Aug 07, 2001
Posts: 13
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

Joined: Jan 09, 2001
Posts: 1492
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

Joined: May 10, 2001
Posts: 265
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

Joined: Nov 05, 2000
Posts: 761

And if you still have withdrawal symptoms from not seeing "this" somewhere in the method call, do this (no pun intended) instead:

Pho


Regards,

Pho
 
 
subject: Why is it wrong to use this() in an overloading method?
 
Similar Threads
Q about Overload Method
"overload the method"question required
Overloaded functions?
overloading -> cannot have different return type?
Overload