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
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!
😂 0:54
no i use chatgpt instead
You pretty much skipped
getField(String name)
getMethod(String name, Class... parameterTypes)
which are pretty useful
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
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.
Are you trying to say hes an old guy.
John if possible could you do a video on Spring? Your through explanations would be beneficial to all.
I am from Armenia .I want to become a programmist.I like the way you explain.Simple and understandable.
Some junior is lucky to have you as a mentor
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 👍
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!
Wow, ok this is the first time i have watched the whole promotion on any video. I just realized this.
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).
this is cursed behavior
if this API contains your code only why do you use reflection (your code is in front of you)
@@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.
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
I've seen a lot of educational videos for Java but the way you explain stuff is a next level! Thank you :)
That's the best video regarding Java Reflection I've watched so far. Everything was clearly explained. Well done, mate.
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!!
I am so grateful for these java tutorials. Not too beginner like a majority of tutorials out there, and very well explained
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!
amazing video ,explain reflection thoroughly,so much easier understand than all the other guides out there on line
Thank you for this. You've given me a lot to reflect on.
(☞゚ヮ゚)☞
☜(゚ヮ゚☜)
The Better Call Saul reference really put a smile on my face. Nice cat name. Haha
Wunderbar. Maximum respect for explaining this in such a simple way
By far the best explanation video I have seen on reflection. Excellent work, much appreciated!
Your explanation is awesome!! Thanks!
Thanks for clear explanation.
Mindblowing lesson!!! Never found this anywhere on the web. Thanks a lot.
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!!
Always love some Better call saul references in my coding 😀
Better Call Saul, Breaking Bad, Seinfeld - I sprinkle in whatever I can.
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!
you simply the best guide i've ever seen in java
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
You are without a doubt one of the best people teaching java! Thank you.
Your videos are amazing, thank you!
Wow thats powerful stuff, this is the first time I have tried to understand reflection in any detail. Thanks for a great video.
Fantastic explanation,never heard about that. I loved the reference to better call Saul!
Thank you! this was amazingly clear
Hey John you're the man! It's pleasant to watch your videos. I'm considering to subscribe to your java class.
That was a cool simple and very useful tutorial. Thank you very much.
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 :)
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!
Wonderful content, as always. Thank you infinitely!
Great explanation! Thanks, John!
the guidebook did, and now I finally understand the chanics!
Nice video mate! And a great way to explain it too. Thanks for share your knowledge.
Thank you for all your lectures.
Great explanation, loved it, thanks :)
You just make eveything simple. Loved it. Do more videos :)
you are a good teacher !! thanks...
Very well explained. Now my morning is good indeed.
I just gained a new superpower, this is an amazing introduction to reflections!
This is super interesting, I can especially see myself use this for testing classes
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!
Thank you John!
awesome explanation, thank you
Thank you so much John, you are a great teacher.. If it's possible for you, please create a video on aspect oriented programming.
I really love your contents! You explain very well. Hoping for more good contents ❤️
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.
Thank you! You are wonderful teacher👍
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.
Thanks a lot for this great knowledge
Hi John, thank you for share knowledge!
After a couple of time, I got to understand what reflection in Java means 😊
Great explanation, thanks you so much on helping me on my studies :)
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
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.
Great Explanation. Alot easier to follow than a professor.
I have been using reflection (sparingly) for years. I never knew where the name came from until now. Thanks!
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!
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.
you got me with the stack overflow bit.
Yeah... Happens to all of us.
So so so helpful, you are amazing
Amazing! thank you
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!
Cool Video Title man , i love your videos!!
Thank you so much
BEST TUTORIALS EVER!!!!!!!!!!
Hello John! Your videos are quite helpful. Can you do a video on "Modules" in Java?
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!
Wow! This is awesome! Thanks! 👌👍🙏
I love your videos, thank you, its so much fun to try implement your videos' code into some of my projects
Thanks, I'm glad you enjoy it!
This with your annotations video, and I totally understand how SpringBoot calls functions that you had defined with their annotations.
Loving the videos! Would love to see a video on value objects if you get the chance :)
Thank you man , amazing videos :)
Thanks John you're really nice
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?
You are too good to be true! I can learn anything you teach!
Reflection is amazing when creating data structures using generics.
You are really a coding geek. Thank you
Perfect! Tnx a lot
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 ?
Thank you.
great material thx
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
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.
As usual mate, you’re a legend 👏 #keepCoding
I cannot thank you enough for this🙏
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..
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.
Hello John . You are an amazing teacher. Can you please make videos on springboot?
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.
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
amazing! I find it useful when testing private methods. Do you see any obstacles except that program could run slower?
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
God I LOVE YOUR VIDS. I had somewhat of a hard time to understand reflection but now it's clear as ever.
My like is for your video name you described. That reflection... ❤