Get and Set methods... how do they work?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Get and Set methods... how do they work?

  1. #1
    Join Date
    Nov 2004
    Posts
    1

    Get and Set methods... how do they work?

    I know how to add them, but, its just a routine i do, i don't know EXACTLY what they do.. and why do i type them out...


    -------------------------------------------------------------
    public Customer(String newCustname, String newNumber, String newID)
    {
    this.Custname = newCustname;
    this.Number = newNumber;
    this.ID = newID;
    }
    public void setCustname(String newCustname)
    {
    this.Custname = newCustname;
    }
    public void setNumber(String newNumber)
    {
    this.Number = newNumber;
    }
    public void setID(String newID)
    {
    this.ID = newID;
    -----------------------------------------------------

    why do i have to set them up? with a void even? any help much appreciated.(please put in words of english) i am really new

    Jared

  2. #2
    Join Date
    Sep 2004
    Posts
    223
    the idea behind the getter and setter methods is to restrict the access of different variables in different classes.

    What this means is that if you were writing a class that other people would use, you wouldnt not want them to directly change some of the variables in your class, so you write setter methods so that that is the only way they can set certain variables in your class.

    For example: I have a class called Person, it has attributes called hair eyes ,and one called counter.

    When someone uses my class to create a new Person, I only want them to be able to manipulate the hair and eyes variables, I dont want them to be able to change the counter variable, so i have 2 setter methods called setHair() and setEyes(). This way i can declaire my counter variable as private, and only my internal operations can effect the counter variable. No one else!

    As for the getter methods, they are use for pretty much the same reasons, I only want the user of my Person class to have access to the hair and eyes variables, I NEVER want them to see what the value of the counter variable is, so once again, I create the getter methods, like getEyes() and getHair() so they can never see the counter variable!

    As for the void part of the method, this is the return type that the method will give back to the user that called the method. When you call a setter method, you dont want it to return a value, you just want it to set one. When you write the getter methods you will not use void, rather the type of data that will be returned...


    I hope this helps even a little bit
    A kram a day keeps the doctor......guessing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center