Access to parameter names at runtime – Java 8

/, Java 8/Access to parameter names at runtime – Java 8

Java 8 parameter name at runtime :

Java 8 introduced an easier way to access the parameter names of methods and constructors at run time.

By default, the names of parameters should not be added to the compatibility burden of a method or constructor to the class. To store parameter names in a particular .class file, and thus enable the Reflection API to retrieve parameter names, compile the source file with the -parameters option to the javac compiler.

To access method or constructor parameters names at run time, the corresponding java file should be compiled with -parameters compiler argument.

Below example demonstrates how to access constructor and method parameters at run time using java  reflection.

Example :

Demo java class to access method and constructor parameters :

Run demo class from command line :

Output :

If you compile your java file with -parameters only you allow to access parameter names at run time, otherwise parameters names would be like arg0, arg1 etc.

Leave a Reply