Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   Constructor Design (https://www.askmehelpdesk.com/showthread.php?t=206026)

  • Apr 15, 2008, 06:34 AM
    brianthomas77
    Constructor Design
    When you create a class you must create a constructor, right?

    so should you create more than one constructor for every class you create?
    1) no argument constructor to initialize ALL private values in case the user doesn't
    &
    2) all other possible variations depending on what the user may enter?

    Also, is it typical to use GET and SET methods to initialize and retrieve ALL variables within the class (or is this a "beginner's" design methodology)?
  • Apr 16, 2008, 12:42 PM
    jstrike
    This is not a difficult concept but I hope I can explain it without confusing you. If you're familiar with the concept of overloading it's pretty much the same thing except you're overloading the constructor. There's one small exception, if you don't specify a constructor in your class file the JVM will use what's called the default constructor, which is simply a constructor with no parameters. If you specify a constructor that uses parameters and you also want to use a constructor with no parameters then you must specify both constructors.
    Consider the following simple data bean...
    [CODE=Java]
    public class TestBean {
    private String a;
    private String b;

    public String getA() {return a;}
    public void setA(String a) { this.a = a; }

    public String getB() {return b;}
    public void setB(String b) { this.b = b; }
    }
    [/CODE]
    With this bean I can instantiate it simply by doing
    TestBean a = new TestBean();
    even though there's no constructor.

    If I were to add a constructor that took a and b as parameters...
    [CODE=Java]
    public class TestBean {
    private String a;
    private String b;

    public TestBean(String a,String b) {
    this.a=a;
    this.b=b;
    }

    public String getA() {return a;}
    public void setA(String a) { this.a = a; }

    public String getB() {return b;}
    public void setB(String b) { this.b = b; }
    }
    [/CODE]
    Now the code I wrote above won't work because there is no constructor that doesn't take parameters.

    As for doing a constructor for all possible variations, I would recommend you avoid doing that. It's best to keep it simple and just call the setters based on the data you have available.

    As for getters and setters, if you are talking about data/form/cargo/whatever you want to call them beans, then yes, it's considered a best practice to make all variables private and use getters and setters for everything. If you are using Struts then your action form beans must have getters and setters for every variable. For my data beans I also always define a toString() method so you can easily dump the contents out to a log file.

    For the above bean it would just look like this...
    [CODE=Java]public String toString() {
    StringBuffer retVal = new StringBuffer(super.toString());

    retVal.append("\na: ");
    retVal.append(a);
    retVal.append("\nb: ");
    retVal.append(b);
    return retVal.toString();

    }[/code]
    Now you can just do:
    TestBean a = new TestBean();
    System.out.println(a);
    ... or better yet if you're using Log4j...
    logger.debug(a);

    Let me know if you have any questions.

  • All times are GMT -7. The time now is 04:05 AM.