Avatar ·

Why are getters and setters needed if you can just make fields public?

Hi everyone! I just started learning Java and OOP, and I have a probably stupid question. In all textbooks and courses they say: "Always make fields private, and access them through getters and setters." But I don't understand why make it so complicated? Here I have a `Person` class: ```java public class Person { public String name; public int age; } ``` And I can easily write: ```java Person person = new Person(); person.name = "Vasya"; person.age = 25; System.out.println(person.name); ``` It works! So why should I write an extra 20 lines of code: ```java public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; }