Java Reflection Tutorial

Java Reflection Cheat Sheet:

Welcome to my Java Reflection Video Tutorial! I’ve been asked many times to cover Java Reflection lately and in this tutorial I’ll show you pretty much everything you can do with Java Refelection.

Many people are confused by reflection because they think it is a concept or technique. Java Reflection is an API and as soon as you know that it becomes easy. The Java Reflection API is used to manipulate classes and everything in a class including fields, methods, constructors, private data, etc. And, in this tutorial you’ll learn it all!






46 responses to “Java Reflection Tutorial”

  1. wesos de queso Avatar

    I understood everything you said. I can't find a reason to use this API, other than somekind of Java editor. Is possible to create new methods in runtime? That would be insane for configuration files.

  2. Blake Bartenbach Avatar

    Holy try/catch blocks. Probably would have been easier to just add "throws Exception" onto main.

  3. Sh.C Avatar

    So clear! Thanks!

  4. A S I R I Avatar

    Can you determine polymorphism factor solely through reflection

  5. A S I R I Avatar

    Can you determine cyclomatic complexity solely through reflection? Why, or why

  6. Ramid Zahin Khan Avatar

    Oh yeah, but how do you replace methods and make some for the original ones?

  7. Ramid Zahin Khan Avatar

    Very detailed, you should have even more people supporting you!

  8. Dullahan Ahk Avatar

    Love you Derek :p <3 , Thank youuu soo much

  9. Giorgos D Avatar

    Thanks for reflecting your knowledge to us Derek! 🙂

  10. CobraL0rd Avatar

    Everything up to min15 is fine. After that i get issues. This is my output in min18:

    java.lang.NoSuchMethodException: UfoEnemyShip.<init>(EnemyShipFactory)
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.getConstructor(Unknown Source)
    at ReflectionDemo.main(
    Exception in thread "main" java.lang.NullPointerException
    at ReflectionDemo.main(

    You can see my code here:

    If someone can help i would be very grateful. Thanks.

  11. Sushant Gaikwad Avatar

    best reflection tutorial ever! thank you

  12. Shubham Chaudhary Avatar

    very helpful tutorial

  13. maks burkov Avatar

    Good video! Can't find the the next tutorial video for Reflection in the real world scenario..

  14. Edward Ryklin Avatar

    Is it possible to get the name of a variable such as this:

    class EnemyShip{


    EnemyShip starBlaster;

    /// I want to now print out starBlaster because that's the name of the variable. But instead I get the name of the class which is EnemyShip.

    starBlaster.Class.getName() returns "EnemyShip" because that's the class's name, but I want it to return "startBlaster" because that's what I called the variable.

  15. Edward Ryklin Avatar

    This is so useful! I never knew this existed until now! Do we have something similar in other languages like Python or C++?

  16. maneesh nair Avatar

    Excellent tutorial Derek. Awesome.. 🙂

  17. Marine Masmondet Avatar

    Hello Derek! Thanks alot for this fabulous video!
    I would like to know how to get the count of the number of class fields that have primitive types such as int,float and etc.

    Thank you!

  18. Manoj Nandam Avatar

    Everything is great about the concept, Can you consider adding sub titles to you videos

  19. Vijayalakshmi Rakesh Avatar

    Hello Derek.. Thi sis awesome. Can u explain how Spring and Hibernate make use of this api?

  20. Ömer Mert Ünal Avatar

    Derek I love you bro , TY very much <3 ;;

  21. codeKhor Avatar

    great videos man! subscribed

  22. Yaojie Wang Avatar

    Derek you are the best! The reflection api has confused me for quite some time and it's nice to see someone has actually put it all so neatly together in such a helpful way!

  23. Maria Helena Avatar

    how did you set the name and get the name of the EnemyShip when you did not make a getter and setter method for that?

  24. Yazılım Bilimi Avatar

    Really helpful…Thanks Derek < 3

  25. Felipe Elias Avatar

    System.out.println("Hello as you asked!");
    } catch(Exception e){
    Log.error("Damn it", e);
    } finally {
    System.out.println("Thank you for this very useful video =]");

  26. Der Rick Avatar

    What are the pre-requisites to understand this concept? I feel very difficult to understand 🙁

  27. be se Avatar

    so help full thank you

  28. Vitaliy Ostapenko Avatar

    This video is not only about reflection, it`s about RTTI too.

  29. Roy lee Avatar

    As always, this guy is a terrific explainer.

  30. JamesJ30t Avatar

    Good tutorial.   Subscribed!

  31. Nikhil Verma Avatar

    21:23 "put some more try catch blocks!" haha! Awesome tutorial man. I have also learnt a lot from your other design pattern videos. Thanks!

  32. Alex M Avatar

    Awesome tutorial. Thanks for this m8

  33. Roxana Pop Avatar

    It's an amazing video! I had no ideea what reflection meant before watching it, it was indeed a mistery to me. Now I can't wait to work with it :D. Thanks a looooot!!!

  34. Mohammad Nairat Avatar

    before i watch this because i'm kinda in a hurry ,,, does this allow me to write -> compile -> invoke classes/methods during run time ?? please quick answer … because i'm very busy with a lot of things … i don't wanna waste 24 min if it's not the thing i want … btw i do love all your videos Derek you are amazing <3

  35. Jeetendra Ahuja Avatar

    Heya,,, Awesome video (THANKS…. )  but I am wondering for your cheat sheet link, its not working

  36. DutzelKP Avatar

    Nice video! Even understandable for german students. Thanks!

  37. haricharan kanduri Avatar

    Derek , all your tutorial videos are absolutely superb. Thank you for putting these and helping the developer community.

  38. Sam Quigley Avatar

    any one know if i have input coming in through a jtextField in a private method,  how  I should go about accessing the string inside of the method, so i can manipulate out side of that method.?

  39. Gerardo Rosiles Avatar

    Are Java 8 features (lambdas and all that) removing the need for reflection?

  40. David Oliveira Avatar

    Hi Derek! Thanks for your videos! They are really great!
    Greetings from Brazil!

  41. Thomas Petrak Avatar

    Hi Derek, at the end of the video you say you're going to show some examples of usage of the Reflection API. I can't see the next video on reflection. Is there one? Btw, a great video, as always!

  42. Ryan W Avatar

    Hi Derek, thank you so much for making these tutorials. I am 14 years old and I started watching these tutorials about a year ago, and they've really helped me. You take things that normally sound super complex and hard to comprehend, and make it extremely easy to learn. Keep it up!

Leave a Reply

Your email address will not be published. Required fields are marked *