Lately I've started adding this to certain classes in an in-house API:public class MyClass {  // I usually do this with classes I expect to   // be printed out or tested a lot (particularly  // in the early stages of creating the class/package).  //   // Having one field is not a criteria for doing this,  // just a simple example.  And of course there is always  // a default value for the toString field.  private Object oneOtherFieldExample;  private Function<MyClass, String> toString =       myClass -> String.valueOf(oneOtherFieldExample);  ...  public void setToString(Function<MyClass,String> toStringFunction) {    toString = toStringFunction;  }  public String toString() {    return toString.apply(this);  }This has come in handy a couple times but since I'm working on an in-house API I'm wondering Do you all foresee any potential issues I'm missing?Is this reasonably legible and intuitive?Does this or something similar already exist as some kind of pattern (and if so, what is it called)?Since this is especially used for testing and early building, should I add a static way to set the default toString variable to avoid having to set it manually every time I instantiate a new MyClass Object (in the case where thousands are being created)?
Java - Using a Function variable to set the toString() method's return value
java;functions;lambda
First-Class functions and Higher Order FunctionsA programming language has first-class functions if it supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures.  In languages where functions are first-class citizens, functions can be passed as arguments to other functions in the same way as other values (a function taking another function as argument is called a higher-order function).It's a perfectly valid, well-respected technique, especially among the Functional Programming crowd. As you've already discovered, it allows you to change the behavior of a class without changing the class itself.The canonical example of higher-order functions is Map.  The Map function accepts a function as a parameter and iterates over a list, applying the function to each item in the list.void map(int (*f)(int), int x[], size_t n) {    for (int i = 0; i < n; i++)        x[i] = f(x[i]);}Another example of higher-order functions is a comparator.  Comparators are used in sort functions to define how the sort compares one value in the collection to another value for ordering purposes.var result = MergeSort(listOfImaginaryNumbers, ImaginaryNumberComparator);Is this a viable technique for your scenario?To be fair, any given class should already know how to ToString() itself, without any help from the outside world.  If I were designing such a class, and I needed some variation on the ToString() output, I would probably provide some parameter in the constructor that the ToString() method could use to modify its behavior, rather than resorting to first-class functions.  Or I would examine the state of the object to decide how ToString() should behave.  To be genuinely useful, the first-class function would have to have knowledge of your class's internals, which would violate encapsulation.Depending on exactly what you want to accomplish, a Map function might be a suitable alternative.Further ReadingFirst-Class Functions