Java Reflection Tutorial

Поділитися
Вставка
  • Опубліковано 25 гру 2024

КОМЕНТАРІ • 305

  • @ryanw4238
    @ryanw4238 9 років тому +6

    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!

    • @derekbanas
      @derekbanas  9 років тому +1

      ***** You're very welcome :) My goal with every video is to make everything easy to understand. I'm happy I was able to help.

    • @ru2979
      @ru2979 2 роки тому

      holy u 14 and doing coding .lol Wish I could have also done the same 🥺

  • @derekbanas
    @derekbanas  12 років тому +14

    You're welcome :) Reflection isn't that hard. I'm glad to have been of help

  • @headerbidding
    @headerbidding 11 років тому +5

    Thousands of clicks of the suggestion videos finally led me to your channel. Your channel, sir, deserve a 500px*500px banner on the youtube front page, because it is a hidden gem! Your explanation of concepts is so clear and the way you deliver it is so special. Thank you for the amazing tutorials.

  • @pinch-of-salt
    @pinch-of-salt 4 роки тому +10

    I love this so much this breaks everything that Java and Object Oriented Programming stands for.

  • @derekbanas
    @derekbanas  11 років тому +3

    You're very welcome :) I'm very happy that you enjoyed the fast pace. I don't know why nobody else makes fast videos

    • @saurabhsuman4960
      @saurabhsuman4960 4 роки тому

      It is sometimes difficult to catch up though interested on topic.. Specially after mid day meal

  • @derekbanas
    @derekbanas  12 років тому +1

    Thank you :) Reflection is taught in weird ways and I think that is what is confusing. When you approach it as just any other API it becomes easy to understand

  • @derekbanas
    @derekbanas  12 років тому

    Well thank you :) I just really enjoy making the videos and I'm glad there are people out there that like watching them. I look back at my PHP tutorial as a lost opportunity in which could have made it really great. I wasn't getting any views though so I kind of rushed the login script stuff and I shouldn't have done that. I'll fix that in the future. Thanks for the kind words :)

  • @derekbanas
    @derekbanas  12 років тому

    You're very welcome. Spring will be covered soon. I won't stop the java tutorials until I've covered everything

  • @liang-chiwuu4785
    @liang-chiwuu4785 3 роки тому

    Wow eight years later I'm still finding this very helpful, thanks a bunch.

    • @derekbanas
      @derekbanas  3 роки тому

      I'm very happy that you enjoyed it :)

  • @derekbanas
    @derekbanas  12 років тому

    You can create classes dynamically using code generation libraries that are out there. Do a google search for that phrase and you'll find them. Add java to the search phrase. I hope that helps

  • @derekbanas
    @derekbanas  12 років тому

    The Observer design pattern is also know as Model/View, Dependent, Distributor/Listener subscriber pattern. You don't need reflection to implement this pattern. Take a look at my tutorial on the Observer Design Pattern. It should clear some things up.

  • @nugenki
    @nugenki 11 років тому

    I like how you talk. Makes it sound so easy to do this, and every thing is comepletely obvious.

  • @derekbanas
    @derekbanas  11 років тому +1

    Thank you :) In the real world I always give everything very detailed names. My OOD tutorial and the refactoring one are probably the only ones in which I show how I really program

  • @mahiransh9894
    @mahiransh9894 2 роки тому

    I am watching your tutorial after 10 years of uploading of this and it is totally usful... thanks

  • @moonis-ahmed
    @moonis-ahmed 9 років тому

    Started working on a 3rd year Assignment that required Reflection basics to grab infos out of various files. I was having really hard time as to where to start because the concept was pretty vague to me and you just taught me the whole idea in 30 min video . Thanks mate really helped alot 👍

    • @derekbanas
      @derekbanas  9 років тому

      +Moonis Ahmed I'm very happy that I could help :)

  • @derekbanas
    @derekbanas  12 років тому

    Yes sort of. You can change just about anything because reflection allows you to break all the rules. In what way do you want to change them?

  • @micro2wice
    @micro2wice 7 років тому

    Loaded with easy to understand examples. Dissects reflection and lays it bare. Excellent tut on java reflection api.

  • @yousifal-yousifi8877
    @yousifal-yousifi8877 10 років тому +6

    Great series and tutorial style. I like the speed since if I think you are going too fast I can easily rewind. Watching someone who is too slow, especially with coding, is aggravating.
    One tip is to try to make sure the code statements mostly show up on the screen, and not covered by side panels. You can double click on a tab to maximize to show it in all its glory and double click again to return to normal view.
    Thanks again!

    • @derekbanas
      @derekbanas  10 років тому +1

      You're very welcome :) Yes the speed of my tutorials is what makes me different. I'll do my best to improve on keeping the code on the screen.

  • @derekbanas
    @derekbanas  12 років тому +1

    It isn't commonly covered and it is vastly misunderstood for some reason. I like to clear up topics like this :)

  • @derekbanas
    @derekbanas  12 років тому

    I'm sorry, but I don't understand the question. The prototype design pattern is actually used to change an object and its methods dynamically. I'll cover other patterns that do that as well soon

  • @lfelipelias
    @lfelipelias 8 років тому +6

    Try{
    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 =]");
    }

  • @derekbanas
    @derekbanas  12 років тому

    You're very welcome :) I'm glad I'm able to teach about these niche subjects and clear them up a bit

  • @kanpav
    @kanpav 9 років тому +1

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

    • @derekbanas
      @derekbanas  9 років тому +1

      haricharan kanduri Thank you for the nice compliments :)

  • @123japanuser
    @123japanuser 12 років тому

    I was upside down in this. Was ashamed to ask for help to anyone.
    But the Intellectual Generous Genius that you are ,
    you Bailed me out , again :)

  • @derekbanas
    @derekbanas  11 років тому

    Thank you very much for the kind message :) I'm glad you enjoy them. Yes, I'm not that popular, but that is ok because my community is very nice!

  • @7thAttempt
    @7thAttempt 12 років тому

    Thank you so much for this Derek! Exactly what I needed - you're round about watching it more than once, but I'd rather it all be in one video that spilt over several.

  • @derekbanas
    @derekbanas  11 років тому +1

    I normally only use reflection for testing and for personal projects. If I'm working in a team, or on code that has any chance of being updated by someone other then me I avoid it.

  • @derekbanas
    @derekbanas  12 років тому

    No this isn't in reference to ReflectASM. This is about the Java Reflection library. I'll see what I can do about a tutorial on that subject

  • @derekbanas
    @derekbanas  12 років тому

    Don't worry, i won't make the same mistake I made with my PHP tutorial. I'll cover every single Java tool available this time if I have to make Java tutorials for another 6 months. I already plan to cover making Android apps for at least 6 months or more. Everything will be covered by the end :)

  • @CronusVelox
    @CronusVelox 3 роки тому

    Absolutely beautiful tutorial! Loved that the code is easily accessible and not behind a paywall! Learned a lot, thanks!

    • @derekbanas
      @derekbanas  3 роки тому +1

      Thank you for the nice compliment :) I'm very happy you enjoyed it

  • @hoboishjoeishme
    @hoboishjoeishme 10 років тому

    Hey Derek
    Great video covering the basics of Java Reflection. Came here after reading the Java Trail on Reflection. I found your examples much easier to understand as opposed to the ones covered in the documentation which I found a bit contrived. It was worth the head-scratching, but nothing beats a down-to-earth explanation.

    • @derekbanas
      @derekbanas  10 років тому

      Thank you :) I'm very happy that I was able to clear up reflection.

  • @Bliss..
    @Bliss.. 10 років тому +20

    I think your voice amazing. You are explaining everything so well..and that voice adds so much to your lessons.

    • @derekbanas
      @derekbanas  10 років тому

      blissB2 Thank you for the compliment :) I appreciate them greatly.

    • @grantwilson6577
      @grantwilson6577 6 років тому

      Ahhh... you voice... Not creepy

  • @roylee3196
    @roylee3196 9 років тому

    As always, this guy is a terrific explainer.

    • @derekbanas
      @derekbanas  9 років тому +1

      +Roy lee Thank you :) I'm glad I could help

  • @derekbanas
    @derekbanas  12 років тому

    I'm sorry, but I don't understand the question. Are you trying to convert an int into a String? You'd do that like this
    newString = Integer.toString(integerVar)
    If that isn't what you were looking for ask and I'll be happy to solve any questions

  • @derekbanas
    @derekbanas  12 років тому

    That is built into the class and has to do with Threads. I wouldn't worry about it. I talk about threads in other Java tutorials

  • @derekbanas
    @derekbanas  11 років тому

    You are able to normally access any classes in the same package using reflection

  • @aznthanh23
    @aznthanh23 5 років тому

    Thank for doing this video on java reflection api. It’s very comprehensive and well explained.

    • @derekbanas
      @derekbanas  5 років тому

      Thank you 😃 Happy to help

  • @derekbanas
    @derekbanas  12 років тому

    Well the simple way to do this would be through a collection. They allow you to define variable names and values dynamically. To do it in an uncommon way you could use an annotation processor. There are other odd ways to accomplish this, but they mostly wouldn't be good. Why not just use a collection?

  • @derekbanas
    @derekbanas  12 років тому

    Thank you very much :) It isn't to hard to find out how much I make from videos online. I definitely have a job. I run a ton of shopping cart sites and write custom programs

  • @andyhynesvideography
    @andyhynesvideography 10 років тому +1

    Thanks a lot Derek.
    Had some trouble getting the concept into practice, but the video really helped crack it. You're on my awesome list.

  • @derekbanas
    @derekbanas  11 років тому

    You're welcome :) I'm glad to be able to help

  • @nikhilverma4976
    @nikhilverma4976 9 років тому +1

    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!

    • @derekbanas
      @derekbanas  9 років тому

      +Nikhil Verma You're very welcome :)

  • @derekbanas
    @derekbanas  12 років тому

    Thank you :) I love to make complicated topics easy to understand

  • @derekbanas
    @derekbanas  12 років тому

    So you want to know how to compile java to machine code? I've never used Raspberry Pi but it seems like fun :) Have you looked at the GNU Compiler for Java?

  • @derekbanas
    @derekbanas  11 років тому

    You're very welcome :) I'm glad I could help

  • @derekbanas
    @derekbanas  11 років тому +1

    I'm glad I could help :) Thank you

  • @CiaranMTG
    @CiaranMTG 12 років тому

    Fricking love the videos Derek,by far some of the most complete and well done Java tutorials on UA-cam IMHO. Do you have a job? Or do you just make epic tutorials for us everyday :)

  • @derekbanas
    @derekbanas  11 років тому

    Thank you very much :) I'm very happy to be able to help

  • @derekbanas
    @derekbanas  12 років тому +1

    It sounds fascinating! I'll have to check out Raspberry Pi :)

  • @derekbanas
    @derekbanas  12 років тому

    Reflectasm generates code, while the java reflection API doesn't. That is the main difference

  • @peeranpc4262
    @peeranpc4262 3 роки тому

    Thanks, man! Your explanation is very clear...
    It helped me a lot!
    Waiting for more!

  • @BhupinderSingh
    @BhupinderSingh 10 років тому

    Hi Derek,
    This is one of the best tutorial i came through. Great job.

    • @derekbanas
      @derekbanas  10 років тому +1

      Bhupinder Singh Thank you :) I'm glad you liked it.

  • @derekbanas
    @derekbanas  11 років тому

    Thank you :) You're very welcome

  • @theag0039762
    @theag0039762 11 років тому

    Nice. Very Informative and helpful. We have used java reflection in our project in implementing the custom field validator framework to identify and show which fields have bad input to user.

  • @derekbanas
    @derekbanas  11 років тому

    I'm calling the UFOEnemyShip constructor that has the parameters int and string with getConstructor(int.class, String.class).
    With the other I return an array of the constructors and pick the one that has the parameter EnemyShipFactory.
    Does that help?

  • @AbstractAbsorption
    @AbstractAbsorption 11 років тому

    Thanks so much man...this REALLY helped, the entire explanation was quite clear. A bit fast at times, but that's what rewinding is for ;) Subscribed.

  • @derekbanas
    @derekbanas  12 років тому

    Thank you :) This was a long one to make

  • @Gorky25
    @Gorky25 5 років тому +4

    When and why we use reflection? Only for testing?
    Do we use reflection with the same purpose in Spring?

    • @PaulGoux
      @PaulGoux 5 років тому

      Gui libraries will often use reflections for buttons and sliders, as it allows for easier code from the user to instance buttons and to create a link from a button constructor, to a method parameter that needs to be toggled or have its value change.

    • @mrthomaschannelearth
      @mrthomaschannelearth 4 роки тому +1

      Spring Data JPA Repositories use Reflection API in the background to create and execute your database queries.. pretty cool if you ask me!

  • @derekbanas
    @derekbanas  12 років тому +10

    It started as a 52 minute video before editing :)

    • @jasper5016
      @jasper5016 3 роки тому +2

      The way you explain it, I would not mind watching 52 min video. Thanks

  • @derekbanas
    @derekbanas  11 років тому

    Thank you :) That is coming up very soon

  • @derekbanas
    @derekbanas  12 років тому

    I'm not sure what you mean by wait methods? In this tutorial

  • @jacksparrowc7311
    @jacksparrowc7311 3 роки тому

    The way You are doing is really cool man..

  • @derekbanas
    @derekbanas  12 років тому

    Thank you :) Feel free to send me a message in UA-cam, or on my website. I don't check facebook / Twitter very often, but I check YT numerous times a day

  •  8 років тому +5

    Really helpful...Thanks Derek < 3

    • @derekbanas
      @derekbanas  8 років тому

      Your welcome :) I'm glad it helped

  • @UCBCteam
    @UCBCteam 12 років тому

    thanks Derek... i will look at the GNU Compiler for Java... its very interesting project... we have to track the sun and move the solar panel accordingly... thanks once again....

  • @B0bi_007
    @B0bi_007 6 років тому

    What's the point of getting a field's value by passing its (string) name instead of just calling the getter for it? Example on 20:30.
    What use case would suggest using this technique?

  • @derekbanas
    @derekbanas  11 років тому

    You're very welcome :)

  • @dfnoshamps
    @dfnoshamps 9 років тому +1

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

    • @derekbanas
      @derekbanas  9 років тому +1

      David Oliveira You're very welcome :) I'm amazed that people all over the world watch my videos.

    • @renanmachado1674
      @renanmachado1674 7 років тому

      Brasiiiiiiiiiiiiiiil! =)

  • @286xav
    @286xav 12 років тому

    You should man, a lot of people would like it!! Ps: what the difference btw this and ASM

  • @Ketan302
    @Ketan302 7 років тому

    your video really clear my understanding for reflection, thanks

  • @sushantgaikwad1865
    @sushantgaikwad1865 8 років тому

    best reflection tutorial ever! thank you

    • @derekbanas
      @derekbanas  8 років тому

      Thank you :) I'm glad it helped

  • @SosetaFurioasaJr
    @SosetaFurioasaJr 3 роки тому

    Hello, please mention where you create the EnemyShipFactory variable from (where is the class), because I had to go through your cheatsheet to see it was from a different lesson. Thank you!

  • @PavanSibal
    @PavanSibal 4 роки тому

    Good tutorial. We can create instances in factory using reflection classForInstance instead of ifelse.

  • @CANEHURRICANE
    @CANEHURRICANE 4 роки тому

    Discovered you today, good stuff!

    • @derekbanas
      @derekbanas  4 роки тому

      Thank you :) I’m happy the video helped

  • @giorgosd3624
    @giorgosd3624 8 років тому +8

    Thanks for reflecting your knowledge to us Derek! :)

    • @derekbanas
      @derekbanas  8 років тому +5

      That's funny :)

    • @CrimeanBandera
      @CrimeanBandera 6 років тому +1

      He's actually right. I've chosen to follow your course among many others, mainly because of the way you explain things, but also because of your encouraging voice.
      Funny enough, now when I read a book on Java, your voice kind of sounds in my head.
      Thanks a lot for sharing your knowledge in a very easy and enjoyable way!

  • @coreycotton7507
    @coreycotton7507 10 років тому

    Liked the lesson a lot. Just to clarify one point which confused me at first. The two lines of code can be modified slightly for clarity:
    Constructor constructor = null;
    Object constructor2 = null;
    I thought that constructor2 was also an constructor but it is intended to be a Object for the new instance.
    Perhaps:
    Object newInstance = null; // Use newInstance instead of constructor2 in the code

    • @derekbanas
      @derekbanas  10 років тому

      Corey Cotton Sorry about any confusion that caused

  • @HumbertoFioravanteFerro
    @HumbertoFioravanteFerro 6 років тому

    It is a gorgeous tutorial. Thank you so much, Derek!

    • @derekbanas
      @derekbanas  6 років тому

      Thank you very much I'm happy I could help :)

  • @venugopalkambhampati5317
    @venugopalkambhampati5317 10 років тому

    Why does makeShip method not show in the output (pls see 10:50 sec in the video)?
    Btw, I added another void method called testthis and it showed up. I could not force makeShip to show in the output. Even the 'constructor' method does not show up.. not sure why. Any and all explanation is most appreciated. Thank you in advance.

  • @derekbanas
    @derekbanas  12 років тому

    Thank you :)

  • @derekbanas
    @derekbanas  12 років тому

    I do my best. I think this is exactly what you are looking for javaranch . com/journal/200711/Journal200711 . jsp#a4 Tell me if that helps? It might make for a fun tutorial

  • @herp_derpingson
    @herp_derpingson 12 років тому

    10:35 to 10:43 Right bottom of the screen
    Method name: wait
    return Type: void
    parameters
    long

  • @ryanfleming2448
    @ryanfleming2448 4 роки тому

    Your tutorial is awesome. The extra newlines hurt.

    • @derekbanas
      @derekbanas  4 роки тому

      Thank you :) Sorry about that

  • @sridhar2malladi
    @sridhar2malladi 12 років тому

    Amazing presentation...wonderfully designed tutorials ...

  • @subhamraj2500
    @subhamraj2500 5 років тому

    One of the best tutorial , you could have made it available in udemy , this shouldn't be free

    • @derekbanas
      @derekbanas  5 років тому

      I'm very happy you liked it :)

  • @roxanapop1557
    @roxanapop1557 9 років тому

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

    • @derekbanas
      @derekbanas  9 років тому

      +Roxana Pop You're very welcome :) I'm glad I could clear up reflection.

  • @yosoybruno911
    @yosoybruno911 11 років тому

    Excellent tutorial is really clear,I just would like to ask you a quick question. It is possible to reflect at runtime?Let say that I have an application in java and this was compiled in a file called Application.jar, then I open this application, I log in and I open an user interface that were populated with a class giving fields values.
    Then can I reflect this information using reflectio?What I would like to do is a kind of API that scrap fields values & after automate it.Thank you!!

  • @derekbanas
    @derekbanas  11 років тому

    I don't know how I got into the habit of using those catch phrases? It is weird how things happen. I dare not start a video without hello internet though. I did that once and everyone went crazy :)

  • @you3392002
    @you3392002 12 років тому

    I like this thank you so much!!! I know i am asking a big favor but if you get a chance can you cover spring, because make complicated topics easy to understand in less time. Thank again.

  • @misc98765
    @misc98765 12 років тому

    Hi Derek, thanks for the tutorial...where can i ask question if i have related to other topics in java?

  • @Broduspa
    @Broduspa 9 років тому

    Nice video! Even understandable for german students. Thanks!

    • @derekbanas
      @derekbanas  9 років тому +1

      DutzelKP Thank you :) I'm glad I could help.

  • @strawzberry125
    @strawzberry125 12 років тому

    Hi and thank you for this video... would reflection be seen as an alternative to the delegate design pattern which is seen in C++??

  • @9891830620
    @9891830620 12 років тому

    Yes, absolutely right buddy!! Because c++ isn't that much dynamic in nature.

  • @derekbanas
    @derekbanas  12 років тому

    You could do that, but I couldn't explain it in a comment. I'm guessing since he didn't respond he wasn't that interested :)

  • @thulasi4nari
    @thulasi4nari 11 років тому

    Very Clear and Quick...!!
    Thank you..!!

    • @derekbanas
      @derekbanas  11 років тому

      Thank you and you're very welcoeme :)

    • @samq6991
      @samq6991 9 років тому

      Thulasi Prasad 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.

  • @Adapt-wj5gi
    @Adapt-wj5gi 8 років тому

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

  • @SRISHONLINE
    @SRISHONLINE 7 років тому

    @Derek Banas this design pattern seems to be too specific to Java, isn't it? please correct, if I am wrong (since, I would try out the concept in C++)..

  • @strawzberry125
    @strawzberry125 12 років тому

    Sorry I phrased that poorly! I meant that in C++ they use pointers to support the delegate design pattern... Would reflection be the way that java supports delegation?
    Just wondering as studying design patterns in College and we code in java and this is a current question on one of my midterms...just doing a bit of research! Thanks

  • @ryklin1
    @ryklin1 8 років тому +1

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

    • @derekbanas
      @derekbanas  8 років тому +2

      Reflection in Python docs.python.org/3.5/c-api/reflection.html
      Reflection examples in C++ stackoverflow.com/questions/41453/how-can-i-add-reflection-to-a-c-application

  • @grantwilson6577
    @grantwilson6577 6 років тому

    Great video. Easy to follow

    • @derekbanas
      @derekbanas  6 років тому

      Thank you :) I'm very happy to be of help

  • @SanketKorgaonkar1982
    @SanketKorgaonkar1982 7 років тому

    Hi Derek, So I was experimenting with something recently - and I am unable to solve it. From your tutorial you show how we can invoke a method of our choosing dynamically with java reflection API. This is pretty cool. I am looking to dynamically provide code or compiled objects - so I not only want to dynamically choose what to execute but dictate what that method does on the fly. I am trying to write software knows the version numbers of all of its modules and tries to listen to a socket for new messages. The messages can contain modules as java object stored in byte arrays - if the software detects that the software module version on an in coming message is greater/ newer than the one its currently using, it replaces its existing module with the one just received.
    In other words you are able to do a software update/ change functionality without rebuilding and re-deploying the jar. Is this doable ?