| | |
get/set Methods in java
![]() |
•
•
Join Date: Nov 2004
Posts: 4
Reputation:
Solved Threads: 0
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:
I have declared a constructor for my Student class: I have declared some methods in my Student class:
In another method called hashCode, I change the name varable in my Student class to upperCase i.e.
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.
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)
private String name = " "; //Holds the data for name private String course = " "; //Holds the data for course
I have declared a constructor for my Student class:
Java Syntax (Toggle Plain Text)
Student (String studentName, String studentCourse) { name = studentName; course = studentCourse; }//end constructor Student
Java Syntax (Toggle Plain Text)
//This method returns a String with the contents of the variable //name to whatever object calls it public String getName ( ) { return name; }//end method getName //This method allows the contents of name to be changed //to store a different String value, should that be required public void setName (String studentName) { name = studentName; }//end method setName public String getCourse ( ) { return course; }//end method getCourse public void setCourse (String studentCourse) { course = studentCourse; }//end method setCourse
Java Syntax (Toggle Plain Text)
public int hashCode (int numStudents) { int hash = 0; int subtotal = 0; int maxNum = 0; //Convert name to upper case name = name.toUpperCase ( );
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
•
•
Join Date: Sep 2004
Posts: 84
Reputation:
Solved Threads: 1
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.)
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.)
•
•
Join Date: Nov 2004
Posts: 4
Reputation:
Solved Threads: 0
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
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.
•
•
Join Date: Nov 2008
Posts: 2
Reputation:
Solved Threads: 0
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.
•
•
Join Date: Nov 2009
Posts: 1
Reputation:
Solved Threads: 0
0
#10 5 Days Ago
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.
![]() |
Similar Threads
- Conceptual Understand of Get and Set Methods (Java)
- Pls helpme in replacing set and get methods with List (Java)
- Need help Using "get and set methods" (Java)
Other Threads in the Java Forum
- Previous Thread: How to build a simple online bookstore
- Next Thread: AVL tree project
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character chat class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list loops mac map method methods mobile netbeans newbie notdisplaying number online printf problem program programming project properties qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort sql string swing system textfields threads time title tree tutorial-sample update variablebinding windows working xor






