Getter and setter methods:
It can be useful to classify methods according to the kinds of work that they do. Two important kinds are known as 'getters' and 'setters'.
A getter method (also called an 'accessor' method) is one that accesses some attribute of an object. A getter method should not change the state of the object.
To ensure that these methods are easily identified, we will follow the popular convention of beginning their names with the word 'get'. It is normal to follow this with the name of the instance variable whose value we are accessing.
A setter method (also called a 'mutator' method) is one that changes the state of an object by setting the value of an instance variable. Setter methods do not normally return a value.
We will follow the convention that this kind of method will have a name beginning with the word 'set'. It is normal to follow this with the name of the attribute whose value is being set.
These methods are often the first we write for a class. There are no keywords in the language to create them; we just use naming conventions to help identify them.
Getter and setter methods for the User class
Recall that the User class describes a user of an applet, and that we wish to store information about the user's ID, email address and how many times he or she has accessed the applet.
Suppose that we require getter methods for each of the three instance variables defined in User, and setters for only its String
variables. In addition, we need a method that updates the number of times the user has accessed the applet. We can now fill in more of the detail of this class:
public class User
{
private String userID;
private String emailAddress;
private int numOfAccesses;
// constructors have still to be written
// setter methods
public void setUserID (String usIdVal)
{
userID = usIdVal;
}
public void setEmailAddress (String emailAddressVal)
{
emailAddress = emailAddressVal;
}
// getter methods
public String getUserID ()
{
return userID;
}
public String getEmailAddress ()
{
return emailAddress;
}
public int getNumOfAccesses ()
{
return numOfAccesses;
}
// update number of accesses by this user
public void updateNumOfAccesses ()
{
numOfAccesses++;
}
}
The code for the methods in the class is straightforward, the only slightly complicated aspect being the use of the return statement, which terminates a method and passes a value back to the object that invoked the method. The type of the value returned is indicated in the method header. For example, getUserID declares that it returns a String.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Getter and setter methods homework and assignments? Live Getter and setter methods experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Getter and setter methods homework help, java assignment help and Getter and setter methods projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours