Method setters and getters are two methods whose job is to retrieve and fill data into objects.
In OOP we often hear the term encapsulation , where data is wrapped with a modifier
private
so that it cannot be accessed directly from outside the class.
Now this setter and getter method will help us access the data.
Why should it be made like this?
There are several reasons:
- To improve data security;
- To make it easier to control attributes and and methods;
- Classes we can make are read-only and write-only ;
- and flexible: programmers can replace a portion of the code without having to fear impacting other codes.
How to Make a Setter and Getter Method
How to make a setter and getter method is the same as creating a normal method.
Example:
class User {
private String username;
private String password;
// ini method setter
public void setUsername(String username){
this.username = username;
}
public void setPassword(String password){
this.password = password;
}
// ini method getter
public String getUsername(){
return this.username;
}
public String getPassword(){
return this.password;
}
}
Method setters and getters must be given a modifier
public
, because this method will be accessed from outside the class.
The difference between the setter method and getter lies in the return value, parameter, and method content.
The method setter has no return value
void
(empty). Because the task is only to fill data into attributes.
While the getter method has a return value according to the type of data to be retrieved.
The method name setter and getter must begin with
set
for setters and get
for getters.
Can you use Indonesian? for example
isi
for setters and ambil
for getters.
It's okay, but this is not recommended. Because if we work with a team, the other team will have difficulties. Especially in the team using English.
Using the Setter and Getter Method
After we create a setter and getter method, we can access or use it like a normal method.
Example:
// Kode ini ditulis di dalam method main
// membuat objek dari class User
User dian = new User();
// menggunakan method setter
dian.setUsername("dian");
dian.setPassword("kopiJava");
// menggunakan method getter
System.out.println("Username: " + dian.getUsername());
System.out.println("Password: " + dian.getPassword());
Output results:
Quick Tips for Making a Setter and Getter Method in Netbeans
We have set up an example setter and getter for two data attributes.
How do you think there is a lot of data? for example there are 10 attributes that require setter and getter methods.
We can use the generator feature in Netbeans.
How:
First, create the data attribute that will be setter and getter methods like this:
class Book {
private String title;
private String author;
private String price;
private String isbn;
private String year;
}
After that right click, then select Insert Code ... or press the
Alt
+ button Insert
so that a menu will appear like this:
Select Setter and Getter to enter the Setter and Getter generator menus. Then specify the attribute to be setter method and the getter.
Kalik Generate to make it ...
Finish in an instant ...
Generator setter and getter are not only in Netbeans. In IDEs like Intellij and Android Studio there are also.
But maybe the button or menu to access it is different.
0 Komentar untuk "Learning Java OOP: Using Method Setter and Getter for Encapsulation"
Silahkan berkomentar sesuai artikel