Exactly, a
BiConsumerconsumes 2 things. [A] An instance of thePersonclass, and [B] the value for thenamefield to set.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.
lambda - Java - method reference for setter and getter - Stack Overflow
java - Lambda expression for setter - Stack Overflow
java - Using setter methods or direct reference to variable inside constructor? - Stack Overflow
Invoking setter method using java reflection - Stack Overflow
Videos
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. In that case, the best match is to a BiConsumer:
BiConsumer<Student, String> studentNameSetter = Student::setName;
Just to include a concrete example where something like this could be useful:
public static <T extends Serializable> void ifNotNull(Consumer<T> setter, Supplier<T> supplier) {
if (supplier != null && supplier.get() != null) {
setter.accept(supplier.get());
}
}
public static void main(String[] args) {
Model a = new Model();
a.setName("foo");
Model b = new Model();
b.setName("bar");
ifNotNull(b::setName, a::getName);
}
The ifNotNull method receives a setter and a getter but only calls the setter if the result of the getter isn't null.
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.
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.
If you happen to use spring framework, you could use the PropertyAccessorFactory for retrieving an implementation of the PropertyAccessor interface:
Accessing properties directly
PropertyAccessor myAccessor = PropertyAccessorFactory.forDirectFieldAccess(object);
// set the property directly, bypassing the mutator (if any)
myAccessor.setPropertyValue("someProperty", "some value");
Accessing properties through accessors/mutators
If you need to access your properties using their getters and setters, you could use instead the forBeanPropertyAccess method:
PropertyAccessor myAccessor = PropertyAccessorFactory.forBeanPropertyAccess(object);
// a `setSomeProperty()` method will be used
myAccessor.setPropertyValue("someProperty", "some value");
You could use BeanUtils:
Step #1
Customer customer = new Customer();
Step #2
BeanUtils.setProperty(customer,"firstName","Paul Young");
You could iterate all class members using reflection and set values accordingly, assuming customer object has:
private String firstName;
// Getter and Setter are defined
I'm assuming you mean a setter method as in set an object?
private String yourString;
public void setYourString(String yourString) {
this.yourString = yourString;
}
This is basic code though so you probably mean something else?
Let me know.
A setter is a method which sets a value for one of your parameters. E.g. many people say it's not nice to have a public variable in a class:
public class SomeClass {
public int someInt = 0;
}
now you can access the variable someInt directly:
SomeClass foo = new SomeClass();
foo.someInt = 13;
You should rather do something like that:
public class SomeClass {
private int someInt = 0;
// Getter
public int getSomeInt() {
return someInt;
}
// Setter
public void setSomeInt(int someIntNew) {
someInt = someIntNew;
}
}
and access it through:
SomeClass foo = new SomeClass();
foo.setSomeInt(13);
All this is just convention... You could name your setter-method however you want! But getters and setters are a good (and readable) way to define access to your class varaibles as you like it (if you want to make it read-only you could just write the getter, if you don't wan't anybody to read the value you could only write the setter, you could make them protected, etc...)
Tutorial is not really required for this. Read up on encapsulation
private String myField; //"private" means access to this is restricted to the class.
public String getMyField()
{
//include validation, logic, logging or whatever you like here
return this.myField;
}
public void setMyField(String value)
{
//include more logic
this.myField = value;
}
In Java getters and setters are completely ordinary functions. The only thing that makes them getters or setters is convention. A getter for foo is called getFoo and the setter is called setFoo. In the case of a boolean, the getter is called isFoo. They also must have a specific declaration as shown in this example of a getter and setter for 'name':
class Dummy
{
private String name;
public Dummy() {}
public Dummy(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
The reason for using getters and setters instead of making your members public is that it makes it possible to change the implementation without changing the interface. 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.