Home » Java » Core Java » Java 8 parameter name at runtime

About Biju Kunjummen

Biju Kunjummen

Java 8 parameter name at runtime

Java 8 will be introducing an easier way to discover the parameter names of methods and constructors.

Prior to Java 8, the way to find the parameter names is by turning the debug symbols on at the compilation stage which adds meta information about the parameter names in the generated class files then to extract the information which is complicated and requires manipulating the byte code to get to the parameter names.

With Java 8, though the compilation step with debug symbols on is still required to get the parameter names into the class byte code, the extraction of this information is way simpler and is supported with Java reflection, for eg. Consider a simple class:

public class Bot {
    private final String name;
    private final String author;
    private final int rating;
    private final int score;

    public Bot(String name, String author, int rating, int score) {
        this.name = name;
        this.author = author;
        this.rating = rating;
        this.score = score;


theoretically, a code along these lines should get hold of the parameter names of the constructor above:

Class<Bot> clazz = Bot.class;
Constructor ctor = clazz.getConstructor(String.class, String.class, int.class, int.class);
Parameter[] ctorParameters =ctor.getParameters();

for (Parameter param: ctorParameters) {
    System.out.println(param.isNamePresent() + ":" + param.getName());

Parameter is a new reflection type which encapsulates this information. In my tests with Java Developer Preview (b120), I couldn’t get this to work though!


  • http://openjdk.java.net/jeps/118


Reference: Java 8 parameter name at runtime from our JCG partner Biju Kunjummen at the all and sundry blog.
(0 rating, 0 votes)
You need to be a registered member to rate this.
1 Comment Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

1 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
Biju Kunjummen Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

newest oldest most voted
Notify of
Biju Kunjummen
Biju Kunjummen

The correct compiler flag for the parameter name related information to be added to the byte code is “-parameters”