![]() |
| ||
| get/set Methods in java 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: private String name = " "; //Holds the data for name I have declared a constructor for my Student class: Student (String studentName, String studentCourse)I have declared some methods in my Student class: //This method returns a String with the contents of the variableIn another method called hashCode, I change the name varable in my Student class to upperCase i.e. public int hashCode (int numStudents)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. |
| ||
| 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. |
| ||
| 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.) |
| ||
| 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 |
| ||
| Re: get/set Methods in java brilliant |
| ||
| Re: get/set Methods in java Quote:
|
| ||
| Re: get/set Methods in java Quote:
about Sandwiches99, if you know you're 4 years late... why still post? |
| ||
| Re: get/set Methods in java Quote:
|
| ||
| Re: get/set Methods in java Quote:
|
| ||
| Quote:
|
| All times are GMT -4. The time now is 5:23 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC