This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes getName() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getName()" Watch "getName()" New topic
Author

getName()

taryn heiney
Greenhorn

Joined: Nov 01, 2001
Posts: 1
class Student{
private String name;
private String ssn;
private String dob;
Student(String n, String s, String d){
name = n;
ssn = s;
dob = d;
Student s1 = new Student("dale","123-45-6789","5-18-70");
System.out.println("Their name is: " + s1.name);
}
}

What would be the proper way to access and change the
name variable. Write two methods that would allow us
to do this for the student class. One "accessor" and
the other a "mutator", or getName() and setName();

I would greatly appreciate if someone could help me with this. Thanks! Taryn
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Well, if you want to abide by the laws of encapsulation, then yes, you want a getter and setter. Make all your instance and class variables private, and manipulate them through those methods.
Jason
Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
for example:
public String getName () {
return name;
}
------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
My Java Games, I'm quite proud


Michael J Bruesch<br /><i>I code, therefore I am.</i>
 
GeeCON Prague 2014
 
subject: getName()