So from reading some of the articles, the message i got out of it was being able to modify fields and set values to classes in real time without recompiling.
so is i
Any time you're dealing with a string at runtime and want to treat part of that string as an identifier in the language.
It can also be used to allow language features to be emulated in the language.
Consider the command line java com.example.MyClass
which turns a string into a class name. This doesn't require reflection, because the java
executable can turn a .class
file into code, but without reflection it would not be able to write java com.example.Wrapper com.example.MyClass
where Wrapper
delegates to its argument as in:
class Wrapper {
public static void main(String... argv) throws Exception {
// Do some initialization or other work.
Class> delegate = Class.forName(argv[0]);
Method main = delegate.getMethod("main", String[].class);
main.apply(null, Arrays.asList(argv).subList(1, argv.length).toArray(argv));
}
}