• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}

 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Hey, I'm supposed to be the guide! Wait up! No fair! You have the tiny ad!
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic