Accessor Methods and Mutator Methods
Accessor and Mutator methods are popularly known as get and set methods or simply getters and setters. In this lesson, we will refer to them as set and get methods. The purpose of set and get methods is for achieving Abstraction that surrounds attributes of a class. This practice follows the data hiding part of encapsulation. The deal is that if you have variables declared as private, they won't be accessible without these set and get methods. Earlier on, we also said that declaring your variables as private and providing set and get methods to manipulate them is a very good software Engineering practice. This Lesson practically explains how to work with getters and setters as with to be good enough to move on with Java.
by Esiat Dvd
1 year, 9 months ago

Normally you do not have to make your Instance/ class variables public because you have a gentleman's agreement with OOPs that you will implement Abstraction and Encapsulation. Instead you provide public set and get methods to manipulate  the variable. This lets you change the way the variables are stored, or add extra validation checks.

The lurnQ will show us how to work with set and get methods.

0 Comment
Write comment

In this lurnQ, we will continue to explore getters and setters. As explained above, these methods give us access to our private class variables. Other methods need to access the instance variables in your class. But because you have declared them as private, it means they are only accessible within that class. To access them from outside that class, you have to use set and get methods. Any other program that creates instances of a class will use these methods to access your data members. Note that a member does not need a set method when its value is changed in other methods.

We will have to see this lurnQ so as to master how to use set and get methods.

Learn Java Tutorial 1.48- Getters and Setters In this Java tutorial we will learn ...
0 Comment
Write comment

Get and set method refer to getting and setting these values:Why? Because these instance variables  are private.

  1. //private variable
  2. private String employeeID; //ID for the employee
  3. private String employeeName; //Name of the employee
  4. private int hourlyRate; //the amount in dollars


The get methods (accessor methods) return the value of the member they are associated with.Set  methods set values  for data members.

We also need to know that get/set methods are not compulsory but we include them for data members when other classes require access to them.

Lets see how to create get and set methods here...

Accessors and mutators in Java, and what is their purpose.
0 Comment
Write comment

In Conclusion, Using get and set methods aids maintainability, flexibility and extensibility of your code. And very important: Good design includes encapsulation – to do this you make public accessor methods and force the calling code to use those methods.This must stick.

How do we create get and set methods? To effectively create Java getters and setters in Eclipse, take a look at this lesson video.

How to effectively create Java getters and setters in Eclipse. Easily generate getters and setters ...
0 Comment
Write comment
1. Set and Get Methods
2. Getters and Setters
3. Accessors and mutators
4. How to create Java getters and setters in Eclipse
Login and track your Progress
F Like this Lesson ? Invite your friends to LurnQ.
Recommendation For You
Something messed up. Click Refresh Recommendations button and try again.
Discuss this Lesson
Loading Details
Give us your valuable Feedback
Some error occured, please try again.
Bug Report
Loading Details