get/set Methods in java

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2004
Posts: 4
Reputation: Maureen is an unknown quantity at this point 
Solved Threads: 0
Maureen Maureen is offline Offline
Newbie Poster

get/set Methods in java

 
0
  #1
Nov 1st, 2004
I am new to programming, so please accept my apologies if this question is simple.

I have a class (say called Student).
I want to store the name of the student (name) and the course (course)

I have declared some variables in my Student class:
  1. private String name = " "; //Holds the data for name
  2. private String course = " "; //Holds the data for course

I have declared a constructor for my Student class:
  1. Student (String studentName, String studentCourse)
  2. {
  3. name = studentName;
  4. course = studentCourse;
  5. }//end constructor Student
I have declared some methods in my Student class:
  1. //This method returns a String with the contents of the variable
  2. //name to whatever object calls it
  3. public String getName ( )
  4. {
  5. return name;
  6. }//end method getName
  7.  
  8. //This method allows the contents of name to be changed
  9. //to store a different String value, should that be required
  10. public void setName (String studentName)
  11. {
  12. name = studentName;
  13. }//end method setName
  14.  
  15. public String getCourse ( )
  16. {
  17. return course;
  18. }//end method getCourse
  19.  
  20. public void setCourse (String studentCourse)
  21. {
  22. course = studentCourse;
  23. }//end method setCourse
In another method called hashCode, I change the name varable in my Student class to upperCase i.e.
  1. public int hashCode (int numStudents)
  2. {
  3. int hash = 0;
  4. int subtotal = 0;
  5. int maxNum = 0;
  6.  
  7. //Convert name to upper case
  8. name = name.toUpperCase ( );
And then use the upper case name variable to calculate my hash code.

What I would like to know is this the best way of doing things? Or should I be using the setName method to change the name variable to upper case?

Also, does anyone know of any online resources where I can look at the set/get methods? (My course notes are very limited).

Thanking you in advance.
Last edited by alc6379; Nov 1st, 2004 at 7:44 pm. Reason: added [code] tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,728
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: get/set Methods in java

 
0
  #2
Nov 1st, 2004
get/set methods should be used judiciously. As it is, you could simply make your members public, remove the get/set methods, and the effect would be the same with less code.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 84
Reputation: jerbo is an unknown quantity at this point 
Solved Threads: 1
jerbo jerbo is offline Offline
Junior Poster in Training

Re: get/set Methods in java

 
0
  #3
Nov 1st, 2004
AS Narue says, use get/set judiciously. The greatest power of Java (and any OO programming language that is,) is the ability to 'encapsulate' data (members) to better control how to use that data.

A good example in your above code is if you wish to make the course member 'read-only' you can exclude the set method. This would prevent anyone from making unwanted changes to the course member data. You can still set the course member data internally to the class, but not outside the class (since it would still be a private member variable.)
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 4
Reputation: Maureen is an unknown quantity at this point 
Solved Threads: 0
Maureen Maureen is offline Offline
Newbie Poster

Re: get/set Methods in java

 
0
  #4
Nov 2nd, 2004
Thank you for your reply Jerbo and Narue.

Did some research and found the following:

accessor methods
Normally you don't make variables in your class public. Instead you provide public set and get methods for the variable. This lets you then later change the way the variable is stored, or add extra validation checks.

Method names made up of a member name with a get or set prefix are methods that are required when the members are private and therefore cannot be accessed from outside the class (which is good programming I understand). Any program that creates instances of a class will use these methods to access the members. A member does not need a set method when its value is changed in other methods.

The get/set methods are not compulsory, only include them for members when other classes require access to them.

The get methods (also called an accessor method) return the value of the member they are associated with and the set method (also called a mutator method) sets the member to a new value.

Using get and set methods aids maintainability, flexibility and extensibility. Good design includes encapsulation – to do this you make public asscessor methods and force calling code to use those methods. I.e. set and get
Last edited by alc6379; Nov 2nd, 2004 at 4:02 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 49
Reputation: ohyeah is an unknown quantity at this point 
Solved Threads: 1
ohyeah ohyeah is offline Offline
Unverified User

Re: get/set Methods in java

 
0
  #5
Apr 8th, 2008
brilliant
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 2
Reputation: sandwiches99 is an unknown quantity at this point 
Solved Threads: 0
sandwiches99 sandwiches99 is offline Offline
Newbie Poster

Re: get/set Methods in java

 
0
  #6
Nov 25th, 2008
Originally Posted by Narue View Post
get/set methods should be used judiciously. As it is, you could simply make your members public, remove the get/set methods, and the effect would be the same with less code.
I know I'm 4 years late, but I found this page... I forget what I googled to find it. Direct assignment is easy, but unless you want absolutely any values to be accepted, mutator methods should be used to handle values that could otherwise cause errors at some point in your code. It's just good practice, it makes your code more robust, and it just makes sense in OOP.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: get/set Methods in java

 
0
  #7
Nov 25th, 2008
Originally Posted by ohyeah View Post
brilliant
that you can't read dates? mwa, not so astonishing.

about Sandwiches99, if you know you're 4 years late... why still post?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 2
Reputation: sandwiches99 is an unknown quantity at this point 
Solved Threads: 0
sandwiches99 sandwiches99 is offline Offline
Newbie Poster

Re: get/set Methods in java

 
0
  #8
Nov 25th, 2008
Originally Posted by stultuske View Post
that you can't read dates? mwa, not so astonishing.

about Sandwiches99, if you know you're 4 years late... why still post?
Just putting in my 2 cents. And I love you.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: get/set Methods in java

 
0
  #9
Nov 25th, 2008
Originally Posted by sandwiches99 View Post
JAnd I love you.
you consider that to be Java related?
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: Piachen is an unknown quantity at this point 
Solved Threads: 0
Piachen Piachen is offline Offline
Newbie Poster
 
0
  #10
12 Days Ago
Originally Posted by sandwiches99 View Post
Just putting in my 2 cents. And I love you.
Eventhough something happend years ago, there will always be peoply just starting with programming and experiencing the same issues - like myself for example. So I don't care that something is written with 4 years in between. I'm just happy to find something that might help me further on.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC