DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   get/set Methods in java (http://www.daniweb.com/forums/thread13283.html)

Maureen Nov 1st, 2004 11:43 am
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
        private String course = " ";  //Holds the data for course

I have declared a constructor for my Student class:
        Student (String studentName, String studentCourse)
        {
            name = studentName;
            course = studentCourse;
        }//end constructor Student
I have declared some methods in my Student class:
//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
In another method called hashCode, I change the name varable in my Student class to upperCase i.e.
        public int hashCode (int numStudents)
        {
            int hash = 0;
            int subtotal = 0;
            int maxNum = 0;

            //Convert name to upper case
            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.

Narue Nov 1st, 2004 4:30 pm
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.

jerbo Nov 1st, 2004 4:58 pm
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.)

Maureen Nov 2nd, 2004 2:50 pm
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

ohyeah Apr 8th, 2008 9:21 am
Re: get/set Methods in java
 
brilliant

sandwiches99 Nov 25th, 2008 9:43 am
Re: get/set Methods in java
 
Quote:

Originally Posted by Narue (Post 64543)
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.

stultuske Nov 25th, 2008 10:40 am
Re: get/set Methods in java
 
Quote:

Originally Posted by ohyeah (Post 579950)
brilliant

that you can't read dates? mwa, not so astonishing.

about Sandwiches99, if you know you're 4 years late... why still post?

sandwiches99 Nov 25th, 2008 11:56 am
Re: get/set Methods in java
 
Quote:

Originally Posted by stultuske (Post 744384)
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.

stultuske Nov 25th, 2008 1:12 pm
Re: get/set Methods in java
 
Quote:

Originally Posted by sandwiches99 (Post 744423)
JAnd I love you.

you consider that to be Java related? :D

Piachen Nov 22nd, 2009 6:20 am
Quote:

Originally Posted by sandwiches99 (Post 744423)
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.


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