Results 1 to 4 of 4
Thread: small question about 'this'
- 02-28-2010, 05:06 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 20
- Rep Power
- 0
small question about 'this'
Basically I want to be able to call the StuffToDo class in two ways:
Java Code:StuffToDo(int x, string s);
I want to do this by having two constructors:Java Code:StuffToDo(string s); // should determine x on its own and then do whatever the first constructor does
But, my problem is that I can't do that because the line marked red gives an error: "cannot find symbol: method StuffToDo(int,java.lang.String)"Java Code:public class StuffToDo { public StuffToDo (int x, String str) { blabla; // whatever needs to be done } public StuffToDo (String str) { blabla; // figure out x [COLOR="Red"]this.StuffToDo(x, str);[/COLOR] }
So I've tried:, but it gives me an error as well: "call to this must be the first statement in a constructor".Java Code:this(x, str);
But that would mean that I can't determine x before I call the first constructor.
So finally I tried setting the first constructor to be a public void type, which allows me to use, but that way I won't be able to call the class usingJava Code:this.StuffToDo(x, str);
any more.Java Code:StuffToDo(int x, string s);
Last edited by GPB; 02-28-2010 at 05:09 PM.
- 02-28-2010, 05:16 PM #2
Member
- Join Date
- Feb 2010
- Posts
- 20
- Rep Power
- 0
I think that by using
the program is looking for a method in the class StuffToDo, and not considering the (first) constructor. Is that correct?Java Code:this.StuffToDo(x, str);
But if I use the other method, I won't be able to determine the x beforehand, because the line needs to be the first statement.
Is there maybe another way to do this? (without having to copy everything from the first constructor)
-
Could you have both constructors share a common method?
Java Code:public class StuffToDo { public StuffToDo (int x, String str) { init(x, str); } public StuffToDo (String str) { blabla; // figure out x init(x, str); } private void init(int x, String str) { //.... }
- 02-28-2010, 05:46 PM #4
Member
- Join Date
- Feb 2010
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
A small doubt
By ranganathan in forum New To JavaReplies: 5Last Post: 02-19-2010, 06:18 AM -
small code, need help
By p0rnstar in forum New To JavaReplies: 22Last Post: 01-22-2010, 01:57 PM -
small doubt
By ravi kumar in forum Java SoftwareReplies: 1Last Post: 08-02-2009, 03:50 PM -
A small Question
By Eku in forum JDBCReplies: 7Last Post: 09-01-2008, 06:10 AM -
small error
By ayoood in forum New To JavaReplies: 23Last Post: 05-27-2008, 12:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks