Java Reflection Explained - bɘniɒlqxƎ noiɟɔɘlʇɘЯ ɒvɒᒐ

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 มิ.ย. 2024
  • Learn Reflection in Java! Break every Java rule with Reflection.
    Try Neeva FREE for 1 full month here! neeva.com/codingwithjohn
    00:00 Great Power of Reflection
    02:16 Getting Fields
    05:26 Setting Private Fields
    09:58 Getting Methods
    11:16 Invoking Methods
    15:58 Why Would You Use This?
    17:24 What Could Go Wrong?
    ☕Complete Java course:
    codingwithjohn.thinkific.com/...
    Reflection is one the more intimidating Java topics, because it can feel so confusing. But reflection isn't so tough once you learn the basics of how to do what you want to do. In this video we'll discuss the essentials of reflection in Java to get you started understanding how to bend Java to your will using it!
    Learn or improve your Java by watching it being coded live!
    Hi, I'm John! I'm a Lead Java Software Engineer and I've been in the programming industry for more than a decade. I love sharing what I've learned over the years in a way that's understandable for all levels of Java learners.
    Let me know what else you'd like to see!
    Links to any stuff in this description are affiliate links, so if you buy a product through those links I may earn a small commission.
    📕 THE best book to learn Java, Effective Java by Joshua Bloch
    amzn.to/36AfdUu
    📕 One of my favorite programming books, Clean Code by Robert Martin
    amzn.to/3GTPVhf
    🎧 Or get the audio version of Clean Code for FREE here with an Audible free trial
    www.audibletrial.com/johnclean...
    🖥️Standing desk brand I use for recording (get a code for $30 off through this link!)
    bit.ly/3QPNGko
    📹Camera I use for recording:
    amzn.to/3wlXcmR
    🎙️Microphone I use (classy, I know):
    amzn.to/3AYGdbz
    Donate with PayPal (Thank you so much!)
    www.paypal.com/donate/?hosted...
    ☕Complete Java course:
    codingwithjohn.thinkific.com/...
    codingwithjohn.com

ความคิดเห็น • 331

  • @CodingWithJohn
    @CodingWithJohn  ปีที่แล้ว +44

    Try Neeva free for 1 full month here! neeva.com/codingwithjohn
    There's no way I could fit everything you can do with reflection in one video. So here are some more things to try!
    Method stuff:
    Get a method's name:
    method.getName();
    Get a method's return type:
    method.getReturnType();
    Invoke a method:
    method.invoke(anyParameters, listedLikeThis);
    If it's private or not accessible, put this before it:
    method.setAccessible(true);
    See if a method has an annotation:
    method.isAnnotationPresent(NameOfAnnotation.class);
    Get an annotation from a method:
    method.getAnnotation(NameOfAnnotation.class);
    Get a method's parameter types:
    method.getParameterTypes();
    Get annotations on a method's parameters:
    method.getParameterAnnotations()
    Get the class that declares this method:
    method.getDeclaringClass();
    Get all exceptions the method declares it can throw:
    method.getExceptionTypes();
    Field stuff:
    Get the value of a field:
    field.get(objectToGetThisFieldFrom);
    Get the type of a field:
    Field.getType();
    Set the value of a field:
    field.set(objectToSetThisFieldOn, valueToSetTheFieldTo);
    Get the name of a field:
    field.getName();
    See if a field has an annotation:
    field.isAnnotationPresent(NameOfAnnotation.class);
    Get an annotation from a field:
    field.getAnnotation(NameOfAnnotation.class);
    Get the class that declares this field:
    method.getDeclaringClass();
    Class stuff:
    Get a class's name:
    class.getName();
    See if a class has an annotation:
    class.isAnnotationPresent(NameOfAnnotation.class);
    Get an annotation from a class:
    class.getAnnotation(NameOfAnnotation.class);
    Get declared fields:
    class.getDeclaredFields();
    Get all fields, which will include fields declared in parent classes:
    class.getFields();
    Get declared methods:
    class.getDeclaredMethods();
    Get all methods, which will include fields declared in parent classes:
    class.getMethods();
    This should get you started. Keep exploring to find out what more you can do!

    • @puneetkumarsingh1484
      @puneetkumarsingh1484 ปีที่แล้ว

      😂 0:54

    • @KhalilRahmouni-qe5zs
      @KhalilRahmouni-qe5zs ปีที่แล้ว

      no i use chatgpt instead

    • @ofekn
      @ofekn 11 หลายเดือนก่อน

      You pretty much skipped
      getField(String name)
      getMethod(String name, Class... parameterTypes)
      which are pretty useful

  • @Inkeri94
    @Inkeri94 ปีที่แล้ว +231

    you are without a doubt one of the best people teaching java and especially java core (I am already a working programmer but I like to watch well prepared videos as a way of knowgledge refreshment), keep up the good work man

  • @mushfiqfuad249
    @mushfiqfuad249 ปีที่แล้ว +138

    I can clearly say, John is by far the best Java tutor. I really wish him as my Java teacher back in my university days.

    • @carguy-xv2cl
      @carguy-xv2cl 8 หลายเดือนก่อน +1

      Are you trying to say hes an old guy.

  • @davidx5828
    @davidx5828 ปีที่แล้ว +92

    John if possible could you do a video on Spring? Your through explanations would be beneficial to all.

  • @armenuhiyeghoyan5419
    @armenuhiyeghoyan5419 17 วันที่ผ่านมา +1

    I am from Armenia .I want to become a programmist.I like the way you explain.Simple and understandable.

  • @pikimk6167
    @pikimk6167 ปีที่แล้ว +4

    Some junior is lucky to have you as a mentor

  • @mattgraves3709
    @mattgraves3709 ปีที่แล้ว +17

    Hey John, I have been a software engineer for 12 years but I have a poor memory:) and appreciate your tutorials for my times revisiting concepts.
    I revisit even basic core concepts sometimes.
    As my career progresses, I find myself doing more code review and oversight vs coding myself plus I also use Kotlin (Android) half the time and trying to stay up on the latest language features and concepts as the industry changes is a lifetime of learning and sometimes relearning.
    You have a real knack at teaching and again, much appreciated 👍

    • @CodingWithJohn
      @CodingWithJohn  ปีที่แล้ว +8

      I'm in the same position where I rarely get to code myself anymore at my job. Kind of ironic that the better you get at coding the less you get to actually do it!

  • @neon0wl2107
    @neon0wl2107 ปีที่แล้ว +2

    Wow, ok this is the first time i have watched the whole promotion on any video. I just realized this.

  • @kenna5031
    @kenna5031 ปีที่แล้ว +17

    Reflection is a very powerful tool. I wrote an API to massively simplify the creation of command line user interface/interactivity for Java programs. I could not have done this without reflection (combined with custom annotations).

    • @cozyrain410
      @cozyrain410 ปีที่แล้ว +5

      this is cursed behavior

    • @beerensaft413
      @beerensaft413 ปีที่แล้ว +2

      if this API contains your code only why do you use reflection (your code is in front of you)

    • @makispapa5867
      @makispapa5867 ปีที่แล้ว

      @@beerensaft413 Using reflection you can have the user call java methods directly or modify fields etc via command line. With reflection you can create a more 'direct' scripting system enabling the user to access code directly from within the script. It's very useful.

  • @ofekn
    @ofekn 11 หลายเดือนก่อน +1

    Here are some examples how reflections are used in real applications:
    events - using reflections and annotation you can build an event registry.
    equals - sometimes in the equals method, instead of using instanceof you use this.getClass() == that.getClass() this way you don't allow child classes to be equals.
    generics - when you use generics, you sometimes need the Class, for example Class#getEnumConstants can give you all possible values of an enum class, and Class#isInstance can help you verify fields.
    there is also java code which uses reflections, for example EnumMap has a constructor which accepts Class

  • @Ross-sg3hq
    @Ross-sg3hq ปีที่แล้ว +6

    I've seen a lot of educational videos for Java but the way you explain stuff is a next level! Thank you :)

  • @DanielSColao
    @DanielSColao ปีที่แล้ว +1

    That's the best video regarding Java Reflection I've watched so far. Everything was clearly explained. Well done, mate.

  • @pebble6248
    @pebble6248 ปีที่แล้ว +7

    Your videos are great! I just got home from school, and without even finishing the video I've already shared it to my friend because (1) I know he'll benefit from it, and (2) I don't even have to finish it to know that it's amazing content.
    Thank you John!!

  • @vikingthedude
    @vikingthedude 11 หลายเดือนก่อน

    I am so grateful for these java tutorials. Not too beginner like a majority of tutorials out there, and very well explained

  • @ByronicGuitarist
    @ByronicGuitarist ปีที่แล้ว +1

    Like a month ago I was reading some code at work that was doing reflection stuff and I thought to myself, "damn, I wish John had a Reflection video". Thanks John, you're the best in the game!

  • @yuxiaofei3442
    @yuxiaofei3442 ปีที่แล้ว

    amazing video ,explain reflection thoroughly,so much easier understand than all the other guides out there on line

  • @WhitneyKugel
    @WhitneyKugel ปีที่แล้ว +5

    Thank you for this. You've given me a lot to reflect on.

    • @CodingWithJohn
      @CodingWithJohn  11 หลายเดือนก่อน +2

      (☞゚ヮ゚)☞
      ☜(゚ヮ゚☜)

  • @saahilnayyer6865
    @saahilnayyer6865 ปีที่แล้ว +2

    The Better Call Saul reference really put a smile on my face. Nice cat name. Haha

  • @pradyumn2692
    @pradyumn2692 2 หลายเดือนก่อน

    Wunderbar. Maximum respect for explaining this in such a simple way

  • @Msyoutube38
    @Msyoutube38 2 หลายเดือนก่อน

    By far the best explanation video I have seen on reflection. Excellent work, much appreciated!

  • @theredd1703
    @theredd1703 3 หลายเดือนก่อน

    Your explanation is awesome!! Thanks!

  • @ShermukhammadKarimov
    @ShermukhammadKarimov 29 วันที่ผ่านมา

    Thanks for clear explanation.

  • @rastkostajic5525
    @rastkostajic5525 2 หลายเดือนก่อน

    Mindblowing lesson!!! Never found this anywhere on the web. Thanks a lot.

  • @ObieR
    @ObieR ปีที่แล้ว

    Dude I cannot thank you enough for your incredible work. The way you break down these concepts make them so easy to understand. Would be amazing if could do a video about RX Java!!

  • @idandrori
    @idandrori ปีที่แล้ว +3

    Always love some Better call saul references in my coding 😀

    • @CodingWithJohn
      @CodingWithJohn  ปีที่แล้ว +1

      Better Call Saul, Breaking Bad, Seinfeld - I sprinkle in whatever I can.

  • @Andrew94750
    @Andrew94750 ปีที่แล้ว +2

    I used reflection a year ago for AQA API client. John, huge thank you for this video! This video helped me to remember some things about reflection.
    It would a huge help to see a video about spring injection or SOLID.
    God bless you!

  • @user-xq9cd5zb2r
    @user-xq9cd5zb2r 11 หลายเดือนก่อน

    you simply the best guide i've ever seen in java

  • @user-pt9vi8gx6u
    @user-pt9vi8gx6u ปีที่แล้ว

    I am from Belarus and start to lern JAVA.I not so fine speak English, but you teaching very and very understanding. Thank you. I'm waiting for new lessons

  • @Devivl
    @Devivl ปีที่แล้ว

    You are without a doubt one of the best people teaching java! Thank you.

  • @Beee178
    @Beee178 ปีที่แล้ว +1

    Your videos are amazing, thank you!

  • @semilife
    @semilife 4 หลายเดือนก่อน

    Wow thats powerful stuff, this is the first time I have tried to understand reflection in any detail. Thanks for a great video.

  • @christianmascolo2612
    @christianmascolo2612 ปีที่แล้ว +3

    Fantastic explanation,never heard about that. I loved the reference to better call Saul!

  • @jereramirez7433
    @jereramirez7433 ปีที่แล้ว

    Thank you! this was amazingly clear

  • @LTZMSYAL
    @LTZMSYAL ปีที่แล้ว

    Hey John you're the man! It's pleasant to watch your videos. I'm considering to subscribe to your java class.

  • @princethampan
    @princethampan 3 หลายเดือนก่อน

    That was a cool simple and very useful tutorial. Thank you very much.

  • @maziarchoubineh7787
    @maziarchoubineh7787 11 หลายเดือนก่อน +1

    Thank you very much for the complete narratives. Your videos have not only helped me with Java but also sparked my interest in becoming a developer.God bless you :)

  • @pizz479
    @pizz479 10 หลายเดือนก่อน

    Wow, this is such a fresh shock to me learning Java. I'm studying to switch my stack from Python to Java. Your lesson do help me a lot. Thanks for a good lesson John!

  • @svalyavasvalyava9867
    @svalyavasvalyava9867 ปีที่แล้ว

    Wonderful content, as always. Thank you infinitely!

  • @johnkeck
    @johnkeck ปีที่แล้ว

    Great explanation! Thanks, John!

  • @mrphan529
    @mrphan529 ปีที่แล้ว

    the guidebook did, and now I finally understand the chanics!

  • @Naimadmdp
    @Naimadmdp ปีที่แล้ว

    Nice video mate! And a great way to explain it too. Thanks for share your knowledge.

  • @adamlasry5225
    @adamlasry5225 ปีที่แล้ว

    Thank you for all your lectures.

  • @stevanvieira3665
    @stevanvieira3665 2 หลายเดือนก่อน

    Great explanation, loved it, thanks :)

  • @mihirgore4074
    @mihirgore4074 ปีที่แล้ว

    You just make eveything simple. Loved it. Do more videos :)

  • @HocineFerradj
    @HocineFerradj 7 หลายเดือนก่อน

    you are a good teacher !! thanks...

  • @Prashantable1
    @Prashantable1 10 หลายเดือนก่อน

    Very well explained. Now my morning is good indeed.

  • @DipanjanPatra
    @DipanjanPatra 11 หลายเดือนก่อน

    I just gained a new superpower, this is an amazing introduction to reflections!

  • @mightygreen3364
    @mightygreen3364 ปีที่แล้ว

    This is super interesting, I can especially see myself use this for testing classes

  • @TheGreat8220
    @TheGreat8220 ปีที่แล้ว

    I gotta be honest... This was the first TH-cam video I've ever watched with a paid promotion that actually convinced me to look into the advertised item further. Also, I love all your videos!

  • @ollysalanson9452
    @ollysalanson9452 ปีที่แล้ว

    Thank you John!

  • @daliaabdou4147
    @daliaabdou4147 ปีที่แล้ว

    awesome explanation, thank you

  • @lalankeathauda7414
    @lalankeathauda7414 ปีที่แล้ว

    Thank you so much John, you are a great teacher.. If it's possible for you, please create a video on aspect oriented programming.

  • @charanko3760
    @charanko3760 ปีที่แล้ว

    I really love your contents! You explain very well. Hoping for more good contents ❤️

  • @user-jp5uu6de2n
    @user-jp5uu6de2n 4 หลายเดือนก่อน

    Hi John. I am Ukrainian, my English is so so. But thank God I understand almoust all your speaking. Short and easy to understanding. Thank you Bro.

  • @user-do3df3rj5m
    @user-do3df3rj5m ปีที่แล้ว

    Thank you! You are wonderful teacher👍

  • @simonrozendal7542
    @simonrozendal7542 ปีที่แล้ว +1

    Hey John. I used reflection in my own framework architecture but less as possible, and there was no other option. I used it to for code in the future that doesn't exist yet. So thank you for your tutorial which confirmed is used it on a good way.

  • @aniketnangare904
    @aniketnangare904 9 หลายเดือนก่อน

    Thanks a lot for this great knowledge

  • @davidl6797
    @davidl6797 ปีที่แล้ว

    Hi John, thank you for share knowledge!
    After a couple of time, I got to understand what reflection in Java means 😊

  • @Kabasaurus
    @Kabasaurus ปีที่แล้ว

    Great explanation, thanks you so much on helping me on my studies :)

  • @dlandsvolka4046
    @dlandsvolka4046 ปีที่แล้ว +2

    16:08 IT'S REALLY USEFULL FOR SPIGOT PLUGIN DEVELOPER O_O
    now i can see and change the value of field and modify the game behavior

  • @inao-cz
    @inao-cz ปีที่แล้ว

    Really cool and nicely shown use of Reflections. For example, I've used this to autoload classes where I wanted and was lazy to write new Command(); 20 times.

  • @AntonisKorezelidis
    @AntonisKorezelidis ปีที่แล้ว

    Great Explanation. Alot easier to follow than a professor.

  • @GordonChil
    @GordonChil ปีที่แล้ว

    I have been using reflection (sparingly) for years. I never knew where the name came from until now. Thanks!

  • @mahoneg
    @mahoneg ปีที่แล้ว +11

    As a long-time programmer in many languages, I still love watching your videos. I recommend them to jr developers as well. Warning ** Don't use something just because it seems cool***. Keep it simple and straightforward if you can. It will be easier for you and the people who follow you into your code. I am more of a dog guy but I like cats too!

    • @CodingWithJohn
      @CodingWithJohn  ปีที่แล้ว +7

      Awesome, thanks for recommending them! And you're right. Reflection or otherwise, opt for simplicity, readability, and maintainability over coolness. Though it's fun to know the cool stuff too.

  • @MD90X
    @MD90X ปีที่แล้ว +1

    you got me with the stack overflow bit.

  • @JenHsingHuang
    @JenHsingHuang 10 หลายเดือนก่อน

    So so so helpful, you are amazing

  • @user-md4mx4uq4q
    @user-md4mx4uq4q 8 หลายเดือนก่อน

    Amazing! thank you

  • @lucashenrique6076
    @lucashenrique6076 ปีที่แล้ว

    Thanks for the video John, always with a great explanation! It would be nice if you have commented about the use of reflections with Anotations and some example of it in a Framework.
    See ya!

  • @kbd3879
    @kbd3879 6 หลายเดือนก่อน

    Cool Video Title man , i love your videos!!

  • @edgbaston149
    @edgbaston149 11 หลายเดือนก่อน +1

    Thank you so much

  • @anonimanonim1223
    @anonimanonim1223 ปีที่แล้ว

    BEST TUTORIALS EVER!!!!!!!!!!

  • @amallkrishna
    @amallkrishna ปีที่แล้ว +2

    Hello John! Your videos are quite helpful. Can you do a video on "Modules" in Java?

  • @sankalp.m.patilsmp1472
    @sankalp.m.patilsmp1472 ปีที่แล้ว

    Thank you for sharing these wonderful topics. You are the best Java tutor out there ❤️. Your explanation is very simple and easy to understand.

  •  ปีที่แล้ว

    Thank you for your videos!

  • @MrAikien
    @MrAikien ปีที่แล้ว

    Wow! This is awesome! Thanks! 👌👍🙏

  • @SAED_212
    @SAED_212 ปีที่แล้ว

    I love your videos, thank you, its so much fun to try implement your videos' code into some of my projects

  • @nischalofchrist
    @nischalofchrist ปีที่แล้ว

    This with your annotations video, and I totally understand how SpringBoot calls functions that you had defined with their annotations.

  • @ruicsf
    @ruicsf ปีที่แล้ว

    Loving the videos! Would love to see a video on value objects if you get the chance :)

  • @robertoenriquechavezrodrig731
    @robertoenriquechavezrodrig731 ปีที่แล้ว

    Thank you man , amazing videos :)

  • @tarikuasfaw6405
    @tarikuasfaw6405 ปีที่แล้ว

    Thanks John you're really nice

  • @haltsmaul.
    @haltsmaul. ปีที่แล้ว +2

    Hey John, appreciate your videos as always.
    Since you covered Lambas already, wouldn't it make sense if you were to make a video on method references as well?

  • @mehulsolanki9435
    @mehulsolanki9435 9 หลายเดือนก่อน

    You are too good to be true! I can learn anything you teach!

  • @fiveoneecho
    @fiveoneecho ปีที่แล้ว +1

    Reflection is amazing when creating data structures using generics.

  • @isaacwhiz
    @isaacwhiz ปีที่แล้ว

    You are really a coding geek. Thank you

  • @darjamilova5029
    @darjamilova5029 ปีที่แล้ว

    Perfect! Tnx a lot

  • @avijitbanik531
    @avijitbanik531 ปีที่แล้ว

    Hey man Neeva is awesome.
    I am a beginner to Java and your core concepts are really awesome. May I know your preference of OS to be habituate that with myself and I will have more experience ?

  • @sandeeproy7621
    @sandeeproy7621 7 หลายเดือนก่อน

    Thank you.

  • @hermesmercuriustrismegistu4841
    @hermesmercuriustrismegistu4841 ปีที่แล้ว

    great material thx

  • @alpaslanbek
    @alpaslanbek ปีที่แล้ว +3

    It's always good to know what you can do. But... There is a reason why there are access modifiers. If a method is declared private, it was decided for a reason. Using reflection should be avoided in production code at all cost. But you said it all :) Good content

    • @kennichdendenn
      @kennichdendenn ปีที่แล้ว +1

      Well, when working with legacy code you dont own, sometimes there is a need - especially for testing purposes, so at least not in production.
      Then, it might be the best option, because that code oftentimes was not designed with (modern) testing in mind - but rewriting aint an option, not testing isnt either, and working around that problem leads to bloated and coarse grained tests - which is also not what we want.

  • @foncki
    @foncki ปีที่แล้ว

    As usual mate, you’re a legend 👏 #keepCoding

  • @AshankMishra
    @AshankMishra ปีที่แล้ว

    I cannot thank you enough for this🙏

  • @kandualmahesh3547
    @kandualmahesh3547 ปีที่แล้ว +1

    Can you make an video differentiation between Java eight and 11… Add Java interview questions and also please add creating on micro services with respect to the spring boot..

  • @mrf1yn561
    @mrf1yn561 ปีที่แล้ว +1

    I really like your videos.
    Can you do a video on the java function package i.e. Consumers, Functions etc.
    I see it being used in lot of Netty based projects and yet i don't understand it.

  • @panchanansahoo1463
    @panchanansahoo1463 ปีที่แล้ว

    Hello John . You are an amazing teacher. Can you please make videos on springboot?

  • @Gamer_with_knowledge
    @Gamer_with_knowledge 8 หลายเดือนก่อน

    Hi John , by far you are the best java teacher I have found on youtube.
    I request you to start making videos on Spring-Boot topics as well.
    I swear views will skyrocket gradually.
    ~ Your faithful old subscriber.

  • @shubhamchouksey9904
    @shubhamchouksey9904 ปีที่แล้ว

    I must say that this is the first topic that I have already known before this video is being made 😅. Thanks for bringing this. Maybe it would better to have each design pattern in Java as a video

  • @kamillo27
    @kamillo27 หลายเดือนก่อน

    amazing! I find it useful when testing private methods. Do you see any obstacles except that program could run slower?

  • @AntonyMBenedict
    @AntonyMBenedict ปีที่แล้ว +1

    I have never worked in Java, nor the need to do so (mostly C++/Python). I still watch your videos and enjoy them. You have a very good style. Do you have plans to teach C++ too? :D

  • @cerealhit555
    @cerealhit555 8 หลายเดือนก่อน

    God I LOVE YOUR VIDS. I had somewhat of a hard time to understand reflection but now it's clear as ever.

  • @dineshdixit8206
    @dineshdixit8206 9 หลายเดือนก่อน

    My like is for your video name you described. That reflection... ❤