Results 1 to 4 of 4
  1. #1
    GPB
    GPB is offline Member
    Join Date
    Feb 2010
    Posts
    20
    Rep Power
    0

    Default 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);
    Java Code:
    StuffToDo(string s); // should determine x on its own and then do whatever the first constructor does
    I want to do this by having two constructors:
    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]
        }
    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)"

    So I've tried:
    Java Code:
    this(x, str);
    , but it gives me an error as well: "call to this must be the first statement in a constructor".
    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
    Java Code:
    this.StuffToDo(x, str);
    , but that way I won't be able to call the class using
    Java Code:
    StuffToDo(int x, string s);
    any more.
    Last edited by GPB; 02-28-2010 at 05:09 PM.

  2. #2
    GPB
    GPB is offline Member
    Join Date
    Feb 2010
    Posts
    20
    Rep Power
    0

    Default

    I think that by using
    Java Code:
    this.StuffToDo(x, str);
    the program is looking for a method in the class StuffToDo, and not considering the (first) constructor. Is that correct?

    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)

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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) {
            //....
        }

  4. #4
    GPB
    GPB is offline Member
    Join Date
    Feb 2010
    Posts
    20
    Rep Power
    0

Similar Threads

  1. A small doubt
    By ranganathan in forum New To Java
    Replies: 5
    Last Post: 02-19-2010, 06:18 AM
  2. small code, need help
    By p0rnstar in forum New To Java
    Replies: 22
    Last Post: 01-22-2010, 01:57 PM
  3. small doubt
    By ravi kumar in forum Java Software
    Replies: 1
    Last Post: 08-02-2009, 03:50 PM
  4. A small Question
    By Eku in forum JDBC
    Replies: 7
    Last Post: 09-01-2008, 06:10 AM
  5. small error
    By ayoood in forum New To Java
    Replies: 23
    Last Post: 05-27-2008, 12:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •