Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

reflection without invoking a method  RSS feed

 
Giovanni Montano
Ranch Hand
Posts: 417
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found a tutorial from Jenkov that say:

If you don't know the name at compile time, but have the class name as a string at runtime, you can do like this:

String className = ... //obtain class name as string at runtime Class class = Class.forName(className);

and follow with this example:
Class aClass = ... //obtain Class object.
String simpleClassName = aClass.getSimpleName();


I do not understand what means "have the class name as string at runtime" I guess I should try myself with an IDE, but conceptually it is not clear to me, maybe it means that is the compiler that knows the name of the class.

I would like also to find a book that explain what happens exactly in runtime, and explains more how classes up on the ierarchy as Class, Object, String etc work.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you are writing your own application you know the names of all the classes in your application - this is what they are referring to as knowing the classname at compile time. ie when the compiler is run all the class names are known.
However if you are writing an application which interacts with some as yet unknown code (for example an IDE has no idea what classes will be created using it) then when the code is compiled the names of the classes and interfaces it has to interact with are not known and therefore there needs to be a mechanism to be able to create instances/call methods on these as yet unknown classes - this is where reflection comes in and is what they are referring to knowing the classname at runtime. You can get the classname at runtime by reading data from a file, getting the user to enter the classname etc etc.
 
Stephan van Hulst
Saloon Keeper
Posts: 7806
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To understand where this might be useful, consider an application that allows plugins. When you compile the application, you don't know yet what plugins will be written for it. To be able to use them, the application needs to know the name of the plugin class it's going to use, and then initialize them through reflection.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another place this is used a lot is in Inversion of Control containers such as Spring.
 
Tim Holloway
Bartender
Posts: 18709
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the classic use of Class.forName() method call was in JDBC code.

JDBC defines a standard interface for all SQL database drivers in Java. Which, having dealt with Python and PHP in that regard is a MAJOR plus. But that means that you don't code for a vendor-specific database driver, you code for the JDBC interface.

The driver is instead deduced from the URI used to obtain a connection, for example: "jdbc:postgres://myserver".

The Class.forName method was used to register a specific driver class with the JDBC connection manager so that the connection manager could reference it when parsing JDBC URIs.

Of course, more recently, there are magic entries that can be put into a JDBC driver jar that make that step unneccessary in modern code, but you'll still occasionally come across an anachronism or just plain ancient code.

You can also use Class.forName to determine whether a given class is loadable over the current classpath context, which can be useful when handling optional components or adding a little anti-bugging code.
 
Giovanni Montano
Ranch Hand
Posts: 417
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:When you are writing your own application you know the names of all the classes in your application - this is what they are referring to as knowing the classname at compile time. ie when the compiler is run all the class names are known.
However if you are writing an application which interacts with some as yet unknown code (for example an IDE has no idea what classes will be created using it) then when the code is compiled the names of the classes and interfaces it has to interact with are not known and therefore there needs to be a mechanism to be able to create instances/call methods on these as yet unknown classes - this is where reflection comes in and is what they are referring to knowing the classname at runtime. You can get the classname at runtime by reading data from a file, getting the user to enter the classname etc etc.

Thank you guys,
I was also thinking over the code templates in Eclipse and intelliJ.
Tony your introductory explanation should substitute the Oracle page imo. Now it is clear what does it means " at runtime"!
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, I'm glad I was able to help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!