Show
Accessor and Mutator in Java - IntroductionIn java, public methods that are used to get and set the value of the private variable are refereed as a Accessor and Mutator respectively. They are also referred as a getter and setter method respectively. Some popular IDE's like Netbeans, Eclipse, IntelliJ etc. creates accessor and mutator method automatically. Accessor Method in JavaIn java, Accessor method are used to access the property of an object. In other words, Accessor method act as a mediator to access the private variable by methods outside a class or some other class. Hence, we can say that accessor method is used to provide encapsulation. The Accessor method is also known as a getter method. It is a general convention to write the accessor methods name beginning with the get word. So that they are easily spotted in the class. Accessor method must be declared as public methods. Although, Accessor methods do not accept any parameters. But, they return a value of a private variable. Therefore, it access the private variable and return that from the function. Generally, We have to write more than one accessor method with different names in the class to return private variables. The statement below shows the syntax of Accessor method in Java.
Example 1:In this example, we will initialize the private variable sid and name using constructor and then using getter method to retrieve the values. As, a method can return only one value at a time, we have to write multiple accessors to retrieve the values of private variables.
Output Student id - 12
Student Name - john Mutator Method in JavaIn java, Mutator method are used to change the properties of an object. In other words, Mutator method initialize or change the value of the instance variable of the class or a private variable of the class. Hence, we can say that mutator method is used to provide encapsulation. A Mutator method is also known as a Setter method or Modifiers. It
is a general convention to write the mutator methods name beginning with the set word. So that they are easily spotted in the class. Mutator method must be declared as public methods. Although, Mutator methods do not have any return type. But, they can accept a parameter depending on the data type of private variable. Thereafter, it will access the private variable using keyword this and set its value as received in the parameter. We can write more than one mutator method with different names in
the class.
Example 1:
Output Inside setter method Example 1: Accessor and Mutator methods in Student classIn this example, we are using both accessor and mutator to get and set the personal details of a student. We can use mutator method to restrict storing some invalid data. Here in the example, we
will throw an
Output Student id - 11 Student Name - John Student Contactno - 234998723 Exception in thread "main" java.lang.IllegalArgumentException: Name should be greater than 3 chars long at student.setDetails(student.java:24) at student.main(student.java:38) Example 2: Accessor and Mutator methods in Bank ClassIn this example, we are using both accessor and mutator to get and set the personal details of a customer in a bank. We can use mutator method
to restrict storing some invalid data. Here in the example, we will keep minimum deposit amount as 1000. So, we will throw an
Output Customer Account no - 11335566 Custormer Name - John Custormer Account Type - Savings Customer Balance - 1000.0 Exception in thread "main" java.lang.IllegalArgumentException: Minimum Deposit 1000 required at Bank.setDetails(Bank.java:29) at Bank.main(Bank.java:47) SummaryThe knowledge of Accessor and Mutator is very useful as it implements encapsulation, which is one of the important feature of Object oriented Programming. When we want our class to be used by external clients or external classes, we can build methods for each private data variable using accessor and mutator. Thereafter, we will need to declare public methods to operate on private data. In this tutorial, we covered the way to create accessor and mutator methods in Java with example. All in all, this tutorial, covers everything that you need to know in order to understand the Accessor and Mutator methods in Java. ReferencesAccessor What is another name for the accessor methods?Accessor methods, also called get methods or getters, allow a way to get the value of each instance variable from outside of the class.
What is the difference between a getter method and an accessor method?A getter method allows you to get the value of a field while an accessor method sets the value of the field.
What is an instance method?An instance method is a method that belongs to instances of a class, not to the class itself. To define an instance method, just omit static from the method heading. Within the method definition, you refer to variables and methods in the class by their names, without a dot.
What is the difference between an accessor method and a mutator method quizlet?What is the difference between an accessor method and a mutator method? An accessor provides the client access to data in the object, while a mutator lets the client change the object's state.
|