Conteúdo do Curso
Java Extended
Java Extended
Getter and Setter
The best way to bypass the private access modifier
All Java programmers use constructs called getters and setters.
Getters and setters are methods that follow a specific pattern. They are used to bypass the private
access modifier and effectively manipulate fields from another class.
What do getters and setters do?
Simply put, the setter allows us to assign a value to a specific field protected by the private access modifier, while the getter allows us to retrieve the value from a field protected by the private access modifier.
The syntax for a getter and a setter:
Main
// getter public fieldType getFieldName() { return field; } // setter public void setFieldName(fieldType field) { this.field = field; }
As you can see in the code above, we use the naming convention for the methods getFieldName()
and setFieldName()
. Therefore, if we have a field private String name
and we create a getter and a setter with the names getName()
and setName()
, respectively. It's also worth noting that the getter returns a value of the same type as the name
field, while the setter takes a parameter of the same type as the name
field.
This allows us to access fields that are protected by the private
access modifier.
Let's take a look at an example of accessing a private field from the Person
class in the main
class:
Main
package com.example; public class Main { public static void main(String[] args) { Person bob = new Person(); bob.setName("Bob"); System.out.println(bob.getName()); } } class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
As you can see, we use the setter to set a value for the name
field, and then we use the getter to display the value of the name
field on the screen. Pay attention to the syntax of the getter and setter, as well as the fact that the name
field in the Person
class is protected by the private
access modifier.
Constructor vs Getter/Setter
Which is better to use, initialization through a constructor + overriding the toString()
method, or using getters and setters?
It is definitely better to use getters and setters to access fields protected by the private
access modifier. This provides greater flexibility in the code and improves its readability. When you see the use of the getName()
method in the code, you immediately understand that this method retrieves the field named name
. The same applies when you see the use of the setName()
method, you immediately understand that you are assigning a specific value to the field for the object of the class in which it is used. If other people read your code, they will be happy to see getters and setters.
It is also worth noting that each field requires its own getter and setter. If a class has two fields protected by the private
access modifier, the class should have one getter for each field, meaning two getters and two setters.
Let's take a look at an example where we add an age
field to the Person
class:
Main
package com.example; public class Main { public static void main(String[] args) { Person bob = new Person(); bob.setName("Bob"); bob.setAge(27); System.out.println("Person's name: " + bob.getName() + ", Person's age: " + bob.getAge()); } } class Person { private String name; private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
As you can see, we have created one getter and one setter for each field of the Person
class. In the main
method, we initialized the fields using the setter and displayed their values on the screen using the getter. Using these methods is very convenient, and you will use them frequently in the future.
Obrigado pelo seu feedback!