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

preview_player
Показать описание
Learn Reflection in Java! Break every Java rule with Reflection.
☕Complete Java course:

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

📕 One of my favorite programming books, Clean Code by Robert Martin

🎧 Or get the audio version of Clean Code for FREE here with an Audible free trial

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?

🖥️Standing desk brand I use for recording (get a code for $30 off through this link!)

📹Camera I use for recording:

🎙️Microphone I use (classy, I know):

Donate with PayPal (Thank you so much!)

☕Complete Java course:

Рекомендации по теме
Комментарии
Автор

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:


Get an annotation from a method:


Get a method's parameter types:
method.getParameterTypes();

Get annotations on a method's parameters:


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:


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:


Get an annotation from a field:


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:


Get an annotation from a 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!

CodingWithJohn
Автор

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.

mushfiqfuad
Автор

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

Inkeri
Автор

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

davidx
Автор

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 👍

matt-g-recovers
Автор

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).

kenna
Автор

Some junior is lucky to have you as a mentor

pikimk
Автор

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<K>

ofekn
Автор

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

Ross-sghq
Автор

Practically, this increases my confusion about the purpose of the encapsulation in Java

YassineMohammedAbuRazane
Автор

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!!

pebble
Автор

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

WhitneyKugel
Автор

The best explanation video I've ever seen on Reflection.

AbdullahNaser-hi
Автор

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!

ByronicGuitarist
Автор

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

РоманЯнущик
Автор

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

DanielSColao
Автор

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

vikingthedude
Автор

Encapsulation: "Look how awesome I am with all those security and stability measures".
Those who just make default getters/setters: "Yeah, sure lol".
Encapsulation: "It's not me being out-played it is you not being able to do stuff correctly… lol"
Reflection: <clears its throat>
Encapsulation: "Screw me…".

eugenesmith
Автор

you got me with the stack overflow bit.

MDX
Автор

My favorite trick is to change the underlying fields of String. Then you can change the contents of the char[] array. Then you can use literals of these strings that you have changed. Sysytem.out.println("Yes") shows "No" intead.

franchello
visit shbcf.ru