944,126 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 185686
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 1st, 2004
0

get/set Methods in java

Expand Post »
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:
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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.
Java Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Maureen is offline Offline
4 posts
since Nov 2004
Nov 1st, 2004
0

Re: get/set Methods in java

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 1st, 2004
0

Re: get/set Methods in java

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.)
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
jerbo is offline Offline
84 posts
since Sep 2004
Nov 2nd, 2004
1

Re: get/set Methods in java

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Maureen is offline Offline
4 posts
since Nov 2004
Apr 8th, 2008
0

Re: get/set Methods in java

brilliant
Reputation Points: 10
Solved Threads: 2
Unverified User
ohyeah is offline Offline
49 posts
since Aug 2005
Nov 25th, 2008
0

Re: get/set Methods in java

Click to Expand / Collapse  Quote originally posted by Narue ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sandwiches99 is offline Offline
2 posts
since Nov 2008
Nov 25th, 2008
0

Re: get/set Methods in java

Click to Expand / Collapse  Quote originally posted by ohyeah ...
brilliant
that you can't read dates? mwa, not so astonishing.

about Sandwiches99, if you know you're 4 years late... why still post?
Reputation Points: 938
Solved Threads: 357
Posting Maven
stultuske is offline Offline
2,528 posts
since Jan 2007
Nov 25th, 2008
1

Re: get/set Methods in java

Click to Expand / Collapse  Quote originally posted by stultuske ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sandwiches99 is offline Offline
2 posts
since Nov 2008
Nov 25th, 2008
0

Re: get/set Methods in java

JAnd I love you.
you consider that to be Java related?
Reputation Points: 938
Solved Threads: 357
Posting Maven
stultuske is offline Offline
2,528 posts
since Jan 2007
Nov 22nd, 2009
0
Re: get/set Methods in java
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Piachen is offline Offline
1 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Break for loop to enter while loop?
Next Thread in Java Forum Timeline: Calculator program Java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC