public class Wrap {
private StringBuilder b = new StringBuilder("hi");
public StringBuilder getB() { return b; }
}
// ...
Wrap w = new Wrap();
w.getB().append("!");
System.out.println(w.getB());What is printed?
- Acheck_circle
hi!
- B
!
- C
hi
- D
Compile-time error
Explanation
The getter returns the same mutable reference; appending modifies the underlying StringBuilder.