1. Exactly, a BiConsumer consumes 2 things. [A] An instance of the Person class, and [B] the value for the name field to set.

  2. Yup. The input is an instance of Person, the output is the name of that person.

The key realization is that the getter lambda represents the getter itself without an actual person instance associated with it. If you do want that, you can; something like somePersonInstance::getName is a Producer (takes no inputs and provides an output), whereas something like Person::getName is a Function, takes 1 Person instance and provides an output.

Answer from rzwitserloot on Stack Overflow
🌐
W3Schools
w3schools.com › java › java_encapsulation.asp
Java Encapsulation and Getters and Setters
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To achieve this, you must: ... You learned from the previous chapter that private variables can only be accessed within the same class (an outside class has no access to it). However, it is possible to access them if we provide public get and set methods.
🌐
DZone
dzone.com › data engineering › data › java getter and setter: basics, common mistakes, and best practices
Best Practices for Java Getter and Setter
October 1, 2019 - So the rule of thumb is: Do not return a reference of the original object in the getter method. Instead, it should return a copy of the original object. With primitive types (int, float, double, boolean, char…), you can freely assign/return ...
Discussions

lambda - Java - method reference for setter and getter - Stack Overflow
How do we interpret the java lambda used for setter? BiConsumer functional interface has been used for parameter type for setField and Person::setName was passed as parameter value for the method. More on stackoverflow.com
🌐 stackoverflow.com
August 29, 2022
java - Lambda expression for setter - Stack Overflow
I'm not sure what you mean by creating a lambda expression for the setter. What it looks like you are trying to do is to assign the method reference to a suitable Functional Interface. More on stackoverflow.com
🌐 stackoverflow.com
java - Using setter methods or direct reference to variable inside constructor? - Stack Overflow
I would use the rule: if the class is not final and the method you are calling from the constructor is not private, avoid it. ... While using a setter in the constructor reduces code duplication, calling overrideable methods (ie non final / non private methods) in a constructor is discouraged ... More on stackoverflow.com
🌐 stackoverflow.com
Invoking setter method using java reflection - Stack Overflow
The value is an ArrayList and the setter method is as below: public void setNames(List names){ this.names = names; } A java.lang.NoSuchMethodException is thrown when running this code, but when the setter method parameter type is changed to ArrayList from List it executes fine. More on stackoverflow.com
🌐 stackoverflow.com
🌐
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.
🌐
Great Learning
mygreatlearning.com › blog › it/software development › getter and setter in java
Getter and Setter in Java
September 3, 2024 - If we assign the object reference directly in Setter Method, both these references (from inside and the outside code) are returned to a single object in the memory. So we can provide the copy of the object instead of returning the reference directly.
🌐
CodeJava
codejava.net › coding › java-getter-and-setter-tutorial-from-basics-to-best-practices
Java Getter and Setter Tutorial - from Basics to Best Practices
September 30, 2019 - 5 5 4 3 2 4 5 1 4 3 2 4As you notice, the 2nd element of the array scores is modified outside the setter, at line 5. Because the getter method returns reference of the internal variable scores directly, so the outside code can obtain this reference and makes change to the internal object.Workaround for this case is that, instead of returning the reference directly in the getter, we should return a copy of the object, so the outside code can obtain only a copy, not the internal object.
Find elsewhere
🌐
Scaler
scaler.com › home › topics › what are getter and setter in java?
What are Getter and Setter in Java? - Scaler Topics
May 4, 2023 - Getter is a method in java used to retrieve the value of the data members or variables and Setter is a method in java used to update or set the value of the data members or variables.
🌐
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 - When we assign object reference directly in the setter methods, both these references point to a single object in memory.
🌐
GeeksforGeeks
geeksforgeeks.org › java › getter-and-setter-in-java
Getter and Setter in Java - GeeksforGeeks
June 22, 2023 - // Java Program to Illustrate Getter and Setter // Importing input output classes import java.io.*; // Class 1 // Helper class class GetSet { // Member variable of this class private String name; // Method 1 - Getter public String getName() { return name; } // Method 2 - Setter public void setName(String N) { // This keyword refers to current instance itself this.name = N; } } // Class 2 // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating an object of class 1 in main() method GetSet obj = new GetSet(); // Setting the name by calling setter method obj.setName("Geeks for Geeks"); // Getting the name by calling getter method System.out.println(obj.getName()); } }
Top answer
1 of 5
6

My first thought was to use the setter in the constructor. So if you want to change how the name is stored, or if you want to add any other behavior while setting the name, you just have to change it once.

But thinking just a bit more on this, I think using direct access to the variable is better if the class is not final and the method is not private. Otherwise someone could extend your, override the method, causing your constructor to call their method with unpredictable behavior.

Rule of thumb: If the class is not final, you should only call private methods in the constructor.

2 of 5
4

While using a setter in the constructor reduces code duplication, calling overrideable methods (ie non final / non private methods) in a constructor is discouraged - it can lead to weird bugs when extending a class.

Consider the following case (based off of your example):

public class Object {
   private String name;

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   //Changed this.name = name to setName(name)
   public Object(String name){
       setName(name);
   }
}

With the following subclass:

public class SubObject extends Object {
   private String Id;

   @Override
   public void setName(String name) {
       super.setName(name + Id);
   }

   public SubObject(String name){
       super(name);
       this.id = "1";
   }
}

Creating an instance of SubObject will cause a null pointer, as setName() is called in the constructor, but the implementation of setName() relies on the Id field having been initialized.

Someone extending a class should not have to go check the source code of the super class to ensure the constructor isn't calling overrideable methods.

🌐
Sololearn
sololearn.com › en › Discuss › 606528 › better-understanding-of-getters-and-setters-java
Better understanding of getters and setters - Java | Sololearn: Learn to code for FREE!
I always thought that only the class of the private variable had the power to change it not the other class: https://code.sololearn.com/cup26CZoujE4/#java ... you can have both in one method, true. programmer sometimes just wish to retrieve the value of an instance variable defined outside the method scope however. In such cases, programmers would find getter to be useful because those variables are not local in the method and setting them public to access directly is generally bad programming practices. The correct convention would be to use getter method to return the reference to such instance variable.
🌐
freeCodeCamp
freecodecamp.org › news › java-getters-and-setters
Getters and Setters in Java Explained
January 25, 2020 - public class Vehicle { private String color; // Getter public String getColor() { return color; } // Setter public void setColor(String c) { this.color = c; } } The getter method returns the value of the attribute.
🌐
Silverbaytech
silverbaytech.com › 2016 › 04 › 05 › java-8-lambdas-and-method-references
Silver Bay Technologies » Java 8 Lambdas and Method References
April 5, 2016 - this method on an object, but I don’t necessarily have the specific object around as yet – I’ll provide that object later.” For this, the reference syntax is ClassName::methodName – the same as for a static method. The catch behind this is that taking this approach changes the “shape” of the functional interface. Let’s take the case of a simple setter – a method that takes one input and returns nothing.
🌐
Intellipaat
intellipaat.com › home › blog › getters and setters in java
Getters and Setters in Java - Explained with Examples
August 25, 2025 - Directly assigning an object reference in setters can lead to issues if the object is modified externally. ... Code Copied! ... Use a copy in setters to avoid external changes. ... Code Copied! ... Only add getters and setters for the fields that require external access or modification. Add the Getters and Setters in the fields that have to be accessed or changed from outside of the class. Because using them from outside the class will break encapsulation in Java and can make your code less secure.