• Post Reply Bookmark Topic Watch Topic
  • New Topic

About Class.forName  RSS feed

 
sridhar Ranganathan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need to know about Class.forName,Why we are using?
& give me small example program.Thanks..
Regards,
Sridhar.R
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's particularly useful to dynamically create instances from their fully-qualified path. For example :

Imagine you want to configure whether to output a message in a file or in the console, using a property file :


Using Class.forName, you can instanciate the appropriate class :


This concept is heavily used by Dependency Injection containers like Spring, to dynamically generates instances from their class name.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You use it to load any class into the JVM. It is often used to load the JDBC driver classes. You can also use its getResource() method to get at other files eg images, in the same folder as the .class file.

More details in the API documentation.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!