Ask Experts Questions for FREE Help !
Ask
    brianthomas77's Avatar
    brianthomas77 Posts: 1, Reputation: 1
    New Member
     
    #1

    Apr 15, 2008, 06:34 AM
    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)?
    jstrike's Avatar
    jstrike Posts: 418, Reputation: 44
    Full Member
     
    #2

    Apr 16, 2008, 12:42 PM
    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.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Plastic Product design, Mold design, Plastics, [ 2 Answers ]

What are all the precautions needs to be taken during Nylon molding?

Circuit Design [ 2 Answers ]

Where do I start if I need to make a scematic to create a circuit that has a 100 ohm resistor and it gives 3 1/2 volts - plus or minus 5%. I can use as many 1.5 volt batteries as needed.

I lost my design :( [ 1 Answers ]

Okay, I recently found a tattoo design that I really wanted. It was a pink and black tribal butterfly, which was perfect for me. So I printed it out and everything and I was going to go talk to an artist about putting it on me, but of course before I could go my dumb lost the design. Now I...

Need Help With A Network Design [ 4 Answers ]

Helow everyone, I have a major problem, I am a student and my prof. gave use the problem listed below and he wants an RFP from us giving him a better solutions to what they already have.. Can anyone help me... PLEASE!! Given the company described below with a centralized IT department, analyze...


View more questions Search