+ 8
How the reflection concept in java works can anyone explain this with some great example????
3 Answers
+ 4
s please can I have the code
+ 1
Reflection provides you the ability to handle classes, objects and members runtime. For example, you can list all methods of a class, their types of parameters, return type, do those methods have a certain annotation, or, check for a static field with a certain name, etc..
Reflection is about handling language level structure. I met the best example of its usage in minecraft server code, which instead of using interfaces for event listeners each, use one interface "Listener" and upon its registration the code rolls through the methods of the class, finds those signed with the @EventHandler annotation, and recognizes the event to handle by the type of the only one parameter of that method. And, it works quite well. I can write you some code too, if you want.
+ 1
The minecraft code is a mess to find anything in place and in one view, so I wrote my own code for ya. Give a like, if like it:
https://code.sololearn.com/cZArftXSptQF/#java
I used HashMap for the event handler list groups for each class, however, the minecraft code makes it one step further into complexity. Well, this is a way more user-friend code :) Feel free to ask.