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

Class.forName()  RSS feed

 
venkata subbaiah
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the use of Class.forName()
 
sahmed ahmed
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class.forName() is used to load a class dynamically from within extensions without having to know the actual class name until runtime.
This is one of the Java's strongest features
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A common example of its use is a program that opens up and
reads a "configuration file" (often XML) that contains class names.
The program can use Class.forName(str).newInstance() to instantiate
these classes. Such a program can be configured by editting this
file, without the need to recompile the program.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class.forName() returns a Class object. (There is a class named Class!) This is part of the Reflection API which is a very powerful feature of Java. The above posts describe just a few examples of how the Reflection API can be used. I suggest that you google for "reflection api java" or something similar. There are plenty of examples and tutorials available, including the official one from Sun.

Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!