Re: (LONG) Student Question: constructors and setMethods

From: Brad BARCLAY (bbarclay_at_jsyncmanager.org)
Date: 10/17/03


Date: Fri, 17 Oct 2003 17:05:38 GMT

Gwen Morse wrote:

> How do I include both a constructor and a setBalance method which are
> "both" supposed to initialize the variables for the worker class? This is
> one of the requirements for the program (from what I understand from my
> reading and notes, normally you use one or the other).

        The constructor and a setter method perform different operations.

        The constructor is used to create a new instance of your class. When
oyu call it, the computer will reserve the necessary memory to store all
of your declared instance variables, and will do whatever tasks you
specify in the constructor. The object won't really exist until you
call its constructor.

        A setter method is typically used to set the value of a variable in an
existing object. If the object hasn't been constructed, you can't call
this method -- you still need to construct an object first (by calling
the classes constructor). However, once the object has been
constructed, you'll probably want to be able to change the value of the
variable, and this is where the setter comes in.

        There is nothing wrong with having the constructor setup a variable,
and having a setter in your object. The constructor will setup the
value for the instance only when the instance is created, wheras once
the instance is created, you can _change_ the value by calling the setter.

        This is pretty intrinsic stuff in Java, so if you don't completely
understand it, it's probably a good idea to find a good book that
explains the concepts of (and differences between) classes and objects,
and what the purpose of a constructor is.

        However, as way of an illustrative example, think of a Java class as a
car factory. You can't drive the car factory to school -- you need to
have it build a car for you first, which you can subsequently drive to
school.

        The constructor is the mechanism you use to build a car -- the car is
the "instance" of the Car Factory "class".

        Now assume that your car "instances" all have built-in AM radios. To
be useful, they'll need a "method" you can use to set it to your
favorite radio station. The radio is pre-set to a default station in
the factory (pre-set when the car is built -- ie: "constructed"). You
don't like this station (we'll call it "640") -- you prefer a different
station ("1020"). The "method" used to set the radio station to 1020 is
your "setter", and it takes your favorite station as its input.

        Now, you can't just walk into the factory when it isn't building any
cars and change the station -- the car factory doesn't have a radio.,
it's the "instances" (the cars) that do. It needs to "construct" an
"instance" of a car first, with its default radio station, and then when
it's finished you can hop in and change the radio station to your liking.

        In Java code, this example would look something like this:

        public class Car {
           int radioStation;

           /** Create a new car, pre-set with
             * the specified radio station
             */
           public Car(int radioStn) {
              radioStation = radioStn;
           } // end-constructor

           /** Sets the radio station in an
             * already built car.
             */
           public void setRadioStation(int station) {
              radioStation = station;
           } // end-method
        } // end-class

        As in the example above, you can't change the radio station in your car
until you construct it first. Thus:

        Car myCar = new Car(640); // Creates a new car
        myCar.setRadioStation(1020); // Changes the radio station
        ...drive around a bit, get to a song you don't like...
        myCar.setRadioStation(780); // Change the radio station
                                           again.

        The advantage here of a setter is that you really don't want to have to
go and have the factory make a whole new car just to listen to a
different radio station! You don't generally want to do this (unless
you have vastly more money than brains...)

        Car my1stCar = new Car(640); // New car from the factory...
        ...Ugh! I hate this radio station!...
        Car my2ndCar = new Car(1020); // Another new car from the
                                           factory...
        ...Ah! That's a better station!...

        You now own two cars. Who would buy a new car just to get a different
radio station, when they can change the station in the car they already
have???

        HTH!

Brad BARCLAY

-- 
=-=-=-=-=-=-=-=-=
 From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
The jSyncManager Project:  http://www.jsyncmanager.org


Relevant Pages

  • Re: compile error about auto_ptr
    ... This question, about the rationale of the standard, would be better ... neither intuitive nor very practical (in particular, a templated constructor does not qualify as a formal copy constructor). ... It's just an example of adopting a special case descriptive term as a formal name for a slightly more general category. ... car, a car used to transport goods, essentially a van) is given a very strict definition as a formal term, with the result that some ordinary cars have sub-optimal designs in order to be formally classified as ...
    (microsoft.public.vc.language)
  • Re: Clean up after exception thrown in constructor
    ... I have a class hierarchy where the constructor of a child class can throw an exception, while the constructor of the parent class has some side-effects. ... public void remove{// called when car is no longer used ...
    (comp.lang.java.programmer)
  • Re: Clean up after exception thrown in constructor
    ... I have a class hierarchy where the constructor of a child class can throw an exception, while the constructor of the parent class has some side-effects. ... public void remove{// called when car is no longer used ...
    (comp.lang.java.programmer)
  • Clean up after exception thrown in constructor
    ... I have a class hierarchy where the constructor of a child class can throw an exception, while the constructor of the parent class has some side-effects. ... public void remove{// called when car is no longer used ...
    (comp.lang.java.programmer)
  • Re: Why do people like music that nobody has heard of?
    ... I just spent the last 3 hours in my car trying to find a radio station to listen to. ... Southern California and it would seem that the "pop music" is dominated by banda, ...
    (rec.music.makers.guitar.acoustic)