Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 24th, 2008, 03:45 PM
10.12.07 10.12.07 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 1 10.12.07 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 14 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old October 24th, 2008, 04:47 PM
mrider's Avatar
mrider mrider is offline
<- My daily commute :^)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2005
Location: Earth. Welcome.
Posts: 1,501 mrider User rank is General 11st Grade (Above 100000 Reputation Level)mrider User rank is General 11st Grade (Above 100000 Reputation Level)mrider User rank is General 11st Grade (Above 100000 Reputation Level)mrider User rank is General 11st Grade (Above 100000 Reputation Level)mrider User rank is General 11st Grade (Above 100000 Reputation Level)mrider User rank is General 11st Grade (Above 100000 Reputation Level)mrider User rank is General 11st Grade (Above 100000 Reputation Level)mrider User rank is General 11st Grade (Above 100000 Reputation Level)mrider User rank is General 11st Grade (Above 100000 Reputation Level)mrider User rank is General 11st Grade (Above 100000 Reputation Level)mrider User rank is General 11st Grade (Above 100000 Reputation Level)mrider User rank is General 11st Grade (Above 100000 Reputation Level)mrider User rank is General 11st Grade (Above 100000 Reputation Level)mrider User rank is General 11st Grade (Above 100000 Reputation Level)mrider User rank is General 11st Grade (Above 100000 Reputation Level)mrider User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 18 h 14 m
Reputation Power: 1697
The terms were selected because their programming use closely mirrors their English definition. Refer to this simple class:
Code:
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.


HTH
__________________
A -> B: Ride.

Reply With Quote
  #3  
Old October 24th, 2008, 09:25 PM
tvc3mye's Avatar
tvc3mye tvc3mye is offline
Daniel Schildsky
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2004
Location: KL, Malaysia.
Posts: 1,534 tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 27 m 57 sec
Reputation Power: 1620
Send a message via MSN to tvc3mye Send a message via Yahoo to tvc3mye Send a message via Google Talk to tvc3mye Send a message via Skype to tvc3mye
Facebook
tutorials

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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Accessors, Mutators, and Constructors

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap