Results 1 to 4 of 4
  1. #1
    willemien is offline Member
    Join Date
    Apr 2010
    Location
    london UK
    Posts
    53
    Rep Power
    0

    Default Linking constructors

    Another very basic problem i fear but i cannot figure it out

    simplified:

    what i would like is:


    Java Code:
    class constructor {
        int value1 = 0; 
        int value2 = 0;
    
        constructor (int v1, int v2 ) {
             // test v1 and v2
            this.value1 = v1;
            this.value2 = v2;
         }
    
        constructor (String string) {
             int v1;
             int v2;
             // convert string to v1, v2
             this (v1,v2);   // THIS DOESN'T WORK
        }
    }

    This doesn't work because this(v1, v2) must be the first statement of
    constructor (String string)
    But at that time v1 and v2 are not even initialised. let alone filled.

    And I also don't want to duplicate the tests from constructor (int v1, int v2 ) to other constructors

    But is there an other solution?

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    solution 1:

    Java Code:
    constructor1(int v1, int v2) {
      setValues(v1, v2);
    }
    constructor2(String str) {
      v1, v2 = convert();
      setValues(v1, v2);
    }
    private setValues() {... }
    solution 2 (and I'd prefer this one):

    Java Code:
    class Stuff {
      public static Stuff makeFromString(String str) {
        v1, v2 = convert();
        return new Stuff (v1, v2);
      }
      public Stuff (int v1, int v2) { ... }
    }

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    static conversion methods also work, if you really need it to be a constructor. i.e.
    Java Code:
    private static int convertToV1(String conv){...}
    private static int convertToV2(String conv){...}
    I use this one if I need it as a constructor, but need to call another constructor for whatever reason.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    willemien is offline Member
    Join Date
    Apr 2010
    Location
    london UK
    Posts
    53
    Rep Power
    0

    Default

    Thanks I think i stick to iLuxa's version 1

    I prefer that because version 2 is a bit a hidden constructor
    and i think at my level clarity is important...

    Question how do i make the post [solved]?

Similar Threads

  1. Linking Jframes
    By nonabhai in forum AWT / Swing
    Replies: 2
    Last Post: 03-04-2010, 03:44 PM
  2. linking to a web page
    By Juuno in forum Advanced Java
    Replies: 7
    Last Post: 05-01-2009, 03:47 PM
  3. static linking
    By Nicholas Jordan in forum Advanced Java
    Replies: 35
    Last Post: 03-14-2009, 10:17 PM
  4. linking 2 Jframes
    By suhaib1thariq in forum New To Java
    Replies: 9
    Last Post: 02-13-2009, 02:30 AM
  5. Linking of exe files
    By archu2friends in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-06-2008, 07:08 AM

Posting Permissions

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