In Java it is a convention to access all fields via getter/setters from outside the class. From inside the class, you usually access fields directly. However, you can also access them via getter/setter is you want to.

It is important to know that this is just a convention. Many other programming languages don't have such strict rules or other concepts. So you are not forced to do that. But it is a good practice.

And: Don't mind performance! Using getters/setters will not affect the performance of your app. The JVM/Java is designed to work exactly like this. Every JVM will optimize your code and handle getters/setters in a very effective way. Try to write clear and good readable code.

Answer from Thomas Uhrig on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_encapsulation.asp
Java Encapsulation and Getters and Setters
The get method returns the variable value, and the set method sets the value. Syntax for both is that they start with either get or set, followed by the name of the variable, with the first letter in upper case: public class Person { private ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ getter-and-setter-in-java
Getter and Setter in Java - GeeksforGeeks
June 22, 2023 - Note: This could be avoided by making the number a private variable and utilizing the setNumber method. Using a getter method, on the other hand, is the sole way to read a number's value.
Discussions

java - is getter method call to access variable better than direct variable access within a class - Stack Overflow
I am just curious to know if it advisable within a class instance to access the class variables using getter methods and if there are any tangible performance differences with direct access. Especi... More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - How do getters and setters work? - Stack Overflow
Also, many tools and toolkits that use reflection to examine objects only accept objects that have getters and setters. JavaBeans for example must have getters and setters as well as some other requirements. ... Void is actually necessary here. If you don't have it you get: error: invalid method ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Setter and getter methods - New to Java
I noticed in some java examples they use setter and getter methods to set and retrieve variables or members. What is the advantages and disadvantages of using setter/getter methods? More on forums.oracle.com
๐ŸŒ forums.oracle.com
April 14, 2006
What are the getter and setter methods? - Stack Overflow
Getter and setter methods are frequently used in Java programming. Getter and setter methods in Java are widely used to access and manipulate the values of class fields. Usually, class fields are decorated with a private access specifier. Thus, to access them, public access specifiers are used ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Top answer
1 of 2
12
Rather than just giving access to a variable directly, you can use a method that retrieves the value (the getter), and another that alters the value (the setter). There are a couple reasons you might want to do this. The biggest one is control. If you just have a public variable, anything outside the class can do whatever it wants, even if it isn't technically "right" (like giving something a negative length, as an example). With the getter/setter methods, you can program extra logic to ensure that you have correct values (like throwing an exception if you try to use a negative length). You can also only provide a getter if you wanted, effectively making it so that the variable can only be read, not directly altered (at least outside the class). Another reason is to hide implementation details. If your class is storing time, as an example, you can have setters and getters for time in minutes, time in hours, time in seconds, etc. These getters and setters can use calculations to translate the units between minutes/hours/etc. to whatever is being used by the internal variable, rather than just sticking with one and remembering that the variable is in milliseconds or something. Alternatively, the "internal variable" can just not exist, like the getArea() method of a Rectangle class (meaning you only need to store length and width; though there are optimizations you can do by storing area too, and having the setters for length/width recalculate as needed). Another reason is logging. You can have your getters and setters record the fact that a retrieval or alteration occurred, giving you an easy way to debug (rather than having to find every single place you messed with a particular variable). Getters/setters can also be changed by inheritance, given that they are methods. And even if you aren't doing anything special right now, it is a good idea to have them in case you come back and find that you need to change the logic to use getters/setters. It is a lot easier to add special getter/setter functionality when you are already using them, as opposed to looking for every variable use. ------------------------------ Edit: TL;DR: In Java, a getter is just a method that gets some value (usually from a variable), while a setter is just a method that sets the value of a variable. They have advantages over just accessing the variable directly, since you can add additional logic as needed, like enforcing proper values, logging, etc.
2 of 2
9
u/Ellisander 's explanation is good. I want to clarify one thing, though. A lot of things in programming, like loops, are tools to help you to get the computer to do things. However, some features of programming language are the opposite: they're not there to help you make the computer do something, they're there to help you organize your program better. Programmers have found, through experience, that as programs get larger, if you make all of the variables public then it tends to lead to lots of bugs. Basically if you let any part of the program change anything at any time, it becomes difficult to manage. Not difficult for the computer! The computer literally doesn't care. It's difficult for you, and other programmers who need to read your code. Getters and setters are one tool to help manage the chaos. You don't HAVE to use them, but it's good practice for larger projects.
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ methods in java โ€บ getters and setters
Getters and Setters Methods in Java
July 23, 2024 - Cat's name: Smudge Cat's age: 5 Cat's weight: 4Now another class (Main) can access the Cat fields, but only through getters. Note that getters have the public access modifier, i.e. they are available from anywhere in the program. But what about assigning values? This is what setter methods are for
Find elsewhere
๐ŸŒ
Project Lombok
projectlombok.org โ€บ features โ€บ GetterSetter
@Getter and @Setter
Normally, all text is copied, and @return is moved to the getter, whilst @param lines are moved to the setter. Moved means: Deleted from the field's javadoc. It is also possible to define unique text for each getter/setter. To do that, you create a 'section' named GETTER and/or SETTER.
Top answer
1 of 3
23

In Java it is a convention to access all fields via getter/setters from outside the class. From inside the class, you usually access fields directly. However, you can also access them via getter/setter is you want to.

It is important to know that this is just a convention. Many other programming languages don't have such strict rules or other concepts. So you are not forced to do that. But it is a good practice.

And: Don't mind performance! Using getters/setters will not affect the performance of your app. The JVM/Java is designed to work exactly like this. Every JVM will optimize your code and handle getters/setters in a very effective way. Try to write clear and good readable code.

2 of 3
6

I will answer your question in three parts :

  1. Direct access (public members) is the worst solution :

    A public member can be accessed from outside the class, which for practical considerations means "potentially anywhere". If something goes wrong with a public field, the culprit can be anywhere, and so in order to track down the bug, you may have to look at quite a lot of code.

  2. Encapsulation ( private members ) :

    A private member, by contrast, can only be accessed from inside the same class, so if something goes wrong with that, there is usually only one source file to look at. If you have a million lines of code in your project, but your classes are kept small, this can reduce your bug tracking effort by a factor of 1000.

  3. Getters and Setters are highly Overused :

    Creating private fields and then using the IDE to automatically generate getters and setters for all these fields is almost as bad as using public fields.

    One reason for the overuse is that in an IDE itโ€™s just now a matter of few clicks to create these accessors. The completely meaningless getter/setter code is at times longer than the real logic in a class and you will read these functions many times even if you don't want to.

CONCLUSION:

Use of accessors (getters and setters ) to restrict direct access to field variable is preferred over the use of public fields, however, making getters and setter for each and every field is overkill. It also depends on the situation though, sometimes you just want a dumb data object. Accessors should be added for field where they're really required. A class should expose larger behavior which happens to use its state, rather than a repository of state to be manipulated by other classes.

๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ core java โ€บ significance of getters and setters in java
Significance of Getters and Setters in Java | Baeldung
January 16, 2024 - Simply defining public getters and setters for a private field in a class is equivalent to making the field public without getters and setters. So, itโ€™s always advisable to decide thoughtfully whether to define accessor methods for all the fields or not. In this tutorial, we discussed the pros and cons of using getters and setters in Java.
๐ŸŒ
Medium
medium.com โ€บ zappos-engineering โ€บ getter-setter-the-most-hated-practice-in-java-199e76f3086b
Getter/Setter โ€” the most hated practice in Java. | by Huss Puripunpinyo | Zappos Engineering | Medium
May 19, 2016 - What about Java? Sorry, Java doesnโ€™t have property feature out of the box, and it seems like they donโ€™t have a plan to add it in the near future. At least, not in Java 9 or 10. Nevertheless, you can use a framework that can generate getter and setter methods for you.
๐ŸŒ
Great Learning
mygreatlearning.com โ€บ blog โ€บ it/software development โ€บ getter and setter in java
Getter and Setter in Java
September 3, 2024 - Read the article below to know everything about Getters and Setters in Java. ... A getter method in Java enables us to retrieve or obtain the data of a variable. It returns the value of private members without changing the class type.
๐ŸŒ
Jenkov
jenkov.com โ€บ tutorials โ€บ java-reflection โ€บ getters-setters.html
Java Reflection - Getters and Setters
January 18, 2024 - Getter A getter method have its name start with "get", take 0 parameters, and returns a value.
๐ŸŒ
Oracle
forums.oracle.com โ€บ ords โ€บ apexds โ€บ post โ€บ setter-and-getter-methods-9547
Setter and getter methods - New to Java
April 14, 2006 - I noticed in some java examples they use setter and getter methods to set and retrieve variables or members. What is the advantages and disadvantages of using setter/getter methods?
๐ŸŒ
xperti
xperti.io โ€บ home โ€บ java getters and setters: basics, common mistakes, and best practices
Getters And Setters In Java: Common Mistakes, And How To Avoid Them?
April 28, 2022 - Getters and setters, also known as accessor and mutator in java, are the conventional methods used to retrieve and update the values of a variable, respectively.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 69907401 โ€บ what-are-the-getter-and-setter-methods
What are the getter and setter methods? - Stack Overflow
Getter and setter methods are frequently used in Java programming. Getter and setter methods in Java are widely used to access and manipulate the values of class fields. Usually, class fields are decorated with a private access specifier.
๐ŸŒ
PrepBytes
prepbytes.com โ€บ home โ€บ java โ€บ getter and setter in java
Getter and Setter in Java
October 16, 2023 - Moreover, it upholds an objectโ€™s state integrity, permitting alterations to be carried out exclusively through designated methods. This makes it easier to change the implementation without affecting the code that uses the thing. We use getter and setter in java to access the data that is hidden by encapsulation.
๐ŸŒ
DEV Community
dev.to โ€บ e00049 โ€บ day-32-getter-and-setter-methods-in-java-simple-example-dbo
day-32: Getter and Setter Methods in Java - Simple Example - DEV Community
April 23, 2025 - They are a key part of encapsulation, which hides the internal state of an object and allows controlled access through methods. ... โœ” Control access to private variables โœ” Add validation before setting values โœ” Make code more maintainable ... public class Jewellery_shop { private int gold_rate; // Getter method (must return gold_rate) public int getGoldRate() { return gold_rate; } // Setter method public void setGoldRate(int gold_rate) { this.gold_rate = gold_rate; System.out.println("Gold Rate set to: " + gold_rate); } } public class Customer { public static void main(String[] args) { Jewellery_shop js = new Jewellery_shop(); System.out.println("Initial Gold Rate: " + js.getGoldRate()); // Set new rate js.setGoldRate(90); // Print updated rate System.out.println("Updated Gold Rate: " + js.getGoldRate()); } }