I am very new to Java
and I am trying to understand some concepts. I have been reading a couple of books but I keep getting stumped around the same place. Look at the following code:
package Grade;
import static java.lang.System.out;
import java.util.*;
public class GradeBook {
private String courseName;
public void setCourseName (String name) {
courseName = name;
}
public String getCourseName() {
return courseName;
}
public void Display() // needs a string to run
{
out.println("Welcome to:" + getCourseName() );
}
}
I'm not sure why setCourseName()
needs ( String name ) to be there or where it even gets that value from. If someone could try to explain this in layman terms it will be greatly appreciated.
This class itself never uses
setCourseName
but theoretically it could be used to change the private variablecourseName
.The reason is that you are unable to access the private attribute (courseName) from outside your class. The get and set methods are respectively designed to access (get) and change (set) its value.
You need to use getter and setter methods for this class because the
courseName
field is private; code outside the class cannot access the field directly. Other code that uses the class (usually called client code) would supply the argument value passed tosetCourseName
and would use the value returned bygetCourseName
.With this simple example, it's hard to appreciate why the class would be designed this way. One could just as easily make
courseName
a public field and let client code manipulate it directly. However, as the class becomes more complicated, using the get/set style offers many advantages. Here are some:Using getter/setter methods helps insulate client code from changes to how the class is implemented. For instance, you might decide later that the course name has some sort of internal structure and you want to keep the pieces of the name in separate fields. You would be able to simply change how
setCourseName
andgetCourseName
are implemented and client code would not need to be updated.Automated software generation tools rely on the existence of property getters and setters. With these tools, you can simply model your class as having a "courseName" property and it will generate code that uses the appropriate methods.
You might later decide that changing the course name should have certain side effects (such as updating a course catalog somewhere). Implementing these dependencies can be done by modifying the
setCourseName
method.With object-oriented programming in general, the default design should be to hide the class implementation as much as possible and expose the class only through methods. There are sometimes good reasons to violate this principle, but adhering to it tends to result in more robust software.
Gradebook's Story
GradeBook has a secret. It's the
private
member variable called courseName.Gradebook: No one touches this variable but me!
Knock, knock. It's the Teacher class.
Teacher: Hey, I found out that you're a GradeBook class, let me assign you a course for today. I suppose you have a
courseName
variable?Gradebook: No, I don't! (gulp)
Thinking...Hmmm, how can I keep little my secret but still let her give me that course name? I need that badly.
Gradebook: Okay, you almost got me there. I won't tell you if I have a
courseName
variable, that's too personal. But you can write down the course in the methodsetCourseName
. Is that okay? I'll handle it from there. :)Actually...
No one is forcing you to write the setCourseName. Your program will run just fine. You have the option to make courseName
public
.But and it's a big BUT: it's tantamount to putting your diary in the lawn and letting your neighbor, or any passerby, have their way with it. You wouldn't do that right?
Encapsulation, in layman's terms.
Welcome to Object Oriented Programming. Your member variable courseName private and cannot be accessed from outside.
To provide read access to courseName variable you provide a getter method:
And to provide write access to courseName your provide a setter method:
Here name is being passed as an argument to setter method which is being used to set value into private variable
courseName
Please read about
encapsulation
anddata abstraction
. Somewhere in that program , someone is instantiating theGradeBook
class and thensetting
its member variables . A sample would be :