Blog for Learning

| lesson material | material summary | questions and answers | definitions | types and examples | other information | materi pelajaran | ringkasan materi | pertanyaan dan jawaban | definisi | jenis-jenis dan contoh-contoh | informasi lainnya |

Powered by Blogger.

Pages

Learning Java OOP: Using Method Setter and Getter for Encapsulation

Java Oop Getter Setter
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 privateso that it cannot be accessed directly from outside the class.
Objects with private data
Now this setter and getter method will help us access the data.
Why should it be made like this?
There are several reasons:
  1. To improve data security;
  2. To make it easier to control attributes and and methods;
  3. Classes we can make are read-only and write-only ;
  4. 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.
Difference between setter and getter methods
The method name setter and getter must begin with setfor setters and getfor getters.
Can you use Indonesian? for example isifor setters and ambilfor 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:
Output result setter and getter

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 Altbutton Insert so that a menu will appear like this:
Generator setter and getter menu
Select Setter and Getter to enter the Setter and Getter generator menus. Then specify the attribute to be setter method and the getter.
Generator setter and getter menu
Kalik Generate to make it ...
generator setter and getter
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

 
Template By Kunci Dunia
Back To Top