A Constructor's main purpose is to intialize your object. When you are dealing with non-visual objects, this can be kinda hard to understand, but basically it set's your variables values and allocates memory space for them (not necassarily it that order). An easier way to see this, is if you create a Java application that has a frame, when you code the object, you would put all of the code dealing with how big your frame is, the layout, the elements in it, and all of that in the contructor. If you are familiar with applets at all, basically everything you would include in init() methond in an applet, you would put in a constructor for an application.
Lets say you have a couple JDialogs that are similiar. Would it be best to have overloaded constructors and pass objects of the different dialogs you want or to have separate JDialog classes for each different one. I've created one class for four different Dialogs and my code is getting ugly. I've actually constructed a bunch of "if" statements that control what to do for what is passed to the BasicJDialog class. Instead of overloaded constructors I have private instances of Strings. So if I called new BasicJDialog( "NEW_USER" ). It uses "if" statements to figure out the layout of the NEW_USER object. I'm thinking I've taken the wrong approach. I'm thinking it would probably be better to take the overloaded constructor way. My code would look better and if I ever had another Dialog to create I would just write another overloaded constructer of that object to handle it. Does this sound like the right approach?
I'm confused about constructors too! I just started class, so I hope this doesn't seem too trivial a question, but why use a constructor instead of assigning values right when you declare the variable?
For starters, you can't use direct assignment to set up an object with values supplied by another source. As an example, what about java.awt.Button? You can't very well use direct assignment in the class to give a Button a String label, unless all Buttons should have the same label. ------------------ Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide [This message has been edited by Michael Ernest (edited February 11, 2001).]
Make visible what, without you, might perhaps never have been seen. - Robert Bresson
There are several reasons I can think of straight off for having constructors rather than assigning in the 'calling' program: a) Consistency. With constructors, you KNOW the object will be initialised correctly. b) Encapsulation. What if you decided to modify what variables are in the class? You wouldn't want to modify every bit of code that created the object. c) Organisation. The code that manipulates the class (and that includes setting values) is all in the one place - the classes .java file.
Your mind is under my control .... your will is now mine .... read this tiny ad
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth