|Uses of classForName||Dynamic Class Loading|
|Under The Hood||Sample Code|
|Creating Class Objects||Finding Classes|
|Class Instance Methods||Warning|
|Nested Classes||Learning More|
When you want code to work with a class that does not yet exist, or is not available, at the time of compilation, you define an interface. The base code references only that interface. You tell your clients that any new code must implement that interface. Then at runtime you use Class. forName to load the new class, then use its methods via the interface methods. The Holiday calculator works this way. You can plug in you own code for new holidays without recompiling. Other places you see this provider/plug-in approach are JDBC (Java Data Base Connectivity) drivers and JCE providers.
Class.forName eagerly loads the class if it not already loaded. Inside the JVM there is a HashMap of all the classes that have been previously loaded. So Class. forName takes under a millisecond if the class you have want is already loaded, If not, it might take 15 milliseconds or so to load it. You pay this time penalty only the first time you use the class. Class. forName is still slower than hard coding the name of the class into your code. With hard coding, you avoid repeated HashMap lookups.
classForName is oblivious to any import statements, so you must fully qualify your class names.
The .class syntax is a kludge, especially double.class. It behaves as though it were a read-only static field even though there is no such field, though obviously there must a hidden pointer in the Class object to the class name.
To keep thing simple, when you have variable classes, they all implement some interface, abstract class or base class, in this case HolInfo:
To go the other way, to get the class name from an object, use obj.getClass().getName().
For a practical example of the technique,
Here is some code that dynamically loads classes, given just the unqualified class name. It looks in two different packages. It caches the classes it finds. The public method returns instances of the named class that implement the Macro interface, not Class objects. This code is the guts of the HTML (Hypertext Markup Language) Static Macros engine. The key tools are Class. forName and Class.And if this essay did not overamp your brain already, have a look at this code to detect whether a class has been loaded
available on the web at:
optional Replicator mirror
Please email your feedback for publication, letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : . If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, especially when sending an ad-hominem attack, a rant composed mainly of obscenities or a death threat, please quote the offending passage and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. I can’t very well fix erroneous or ambiguous text if I can’t find it.
Your face IP:[22.214.171.124]
|Feedback||You are visitor number 190,349.|