• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why reflection  RSS feed

 
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question:

Through reflection we can get more details about the class an object belongs to, the methods of that class and the superclass of that class etc. My question is if I can directly go to the required class and check what the methods are , what the super class is etc then what is the need for reflection?

 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The reflection library is generally used (or I believe the original intention was) by tools, such as debuggers or profilers. It is certainly not for common usage in many programs.

And yes, you can write code to access a class variables and methods, but can you do it for a class you don't know about?  Or for a class that don't exist yet? Try to write a program, that takes the name of the class and methods, via parameters -- and instantiate an object, and call a method on it. The class doesn't exist yet, so, for your program, you can't declare a reference variable of that type, can't cast to that type, etc.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!