1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Rep Power

    Accessors, Mutators, and Constructors

    I'm having a difficult time understanding some things for my Programming I class and it's kind of stressing me out.

    I don't understand what a constructor is, don't know what a mutator or an accessor is at all because I missed a class, and have never fully understood what it means to "create a new object".

    My boyfriend tries to help but for some reason, he just can't seem to explain things in a way that makes sense to me. I guess I kind of need examples and things put to me very simply and I'd really appreciate it if someone could help me.
  2. #2
  3. <- My daily commute :^)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2005
    Earth. Welcome.
    Rep Power
    The terms were selected because their programming use closely mirrors their English definition. Refer to this simple class:
    public class Foo {
        private String bar;
        public Foo() { // <- Constructor
            bar = "Default";
        public String getBar() {  // <- Accessor
            return bar;
        public String setBar( String differentBar ) {  // <- Mutator
            this.bar = differentBar;
    public class TestFoo {
        public static void main( String[] args ) {
            Foo foo;                               // #1
            foo = new Foo();                       // #2
            System.out.println( foo.getBar() );    // #3
            foo.setBar( "foobar" );                // #4
            System.out.println( foo.getBar() );    // #5
    At item #1 we've said "I'm going to want a Foo". But we don't actually have a Foo yet. It's like having an empty lot where a custom house will go. At it's most fundamental, the Object "Foo" occupies enough memory on your computer to hold however many characters you put into "bar" (plus a bit of overhead). There's room for it, but it's not there yet. (That's actually somewhat inaccurate, but close enough)

    At item #2 we call the "Constructor". That actually makes a Foo. Basically the construction crew has come to your lot and built your house. Prior to this it was only a concept - a "Foo like house". Now it is there.

    At items #3 & 5 we use the "accessors". We are "accessing" information that's inside of the Foo. It's leaving a Window uncovered in your new house's living room so the neighbors can see your clock. They can't touch the clock, but they can see the time. They can "access" the time.

    At item #4 we use the "mutator". This is analogous to leaving the door unlocked so the neighbor can come in and adjust the time or even replace the clock. We are making a change - and since "changor" sounds too goofy ( ), we use "mutator". Actually, we are mutating the object to look different.

    A -> B: Ride.
  4. #3
  5. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    KL, Malaysia.
    Rep Power


    There are plenty of tutorials available online. Just spend a little time googling the internet and you should find the suitable materials for beginners.
    When the programming world turns decent, the real world will turn upside down.

IMN logo majestic logo threadwatch logo seochat tools logo