Differences between Class.forName and ClassLoader load classes in Java reflection

Source: Internet
Author: User

Explain

Both Class.forName () and ClassLoader in Java can load classes. ClassLoader is the class loader that follows the parent delegation model and finally calls the bootloader to launch the class loader, the function of which is "to obtain a binary byte stream describing this class through the fully qualified name of a class", which is obtained into a binary stream and placed into the JVM. The Class.forName () method is actually also called the ClassLoader to implement the

Class.forName (String className); The source of this method is

&LT;TBODY&GT;&LT;TR&GT;&LT;TD class= "Gutter" ><div class= "line number1 index0 alt2" >1</div><div class= "line number2 index1 alt1" >2</div><div class= "line Number3 index2 alt2" >3</div><div class= "line Number4 index3 alt1" >4</div><div class= "line Number5 index4 alt2" >5</div><div class= "line Number6 index5 alt1" >6</div></td><td class= "code" ><div class= "container" > <div class= "line number1 index0 alt2" ><code class= "java color1" > @CallerSensitive </code></div ><div class= "line number2 index1 alt1" ><code class= "java Spaces" >&nbsp;&nbsp;&nbsp;& Nbsp;</code><code class= "java keyword" >public</code> <code class= "java keyword" >static</ code> <code class= "Java plain" &GT;CLASS&AMP;LT;? &gt; forname (String className) </code></div><div class= "line Number3 index2 alt2" ><code class= "Java Spaces ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& Nbsp;&nbsp;&nbsp;&nbsp;</code><code class= "java keyword" >throws</code> <code Class= "Java plain" >classnotfoundexception {</code></div><div class= "line number4 index3 alt1" > <code class= "java Spaces" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </code><code class= "Java plain" &GT;CLASS&AMP;LT;? &gt; Caller = Reflection.getcallerclass (); </code></div><div class= "line Number5 index4 alt2" ><code Class= "Java Spaces" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</ Code><code class= "java keyword" >return</code> <code class= "Java Plain" &GT;FORNAME0 (ClassName, < /code><code class= "java keyword" >true</code><code class= "Java plain"; Classloader.getclassloader (caller), caller); &LT;/CODE&GT;</div><div class= "line Number6 index5 alt1" ><code class= "java Spaces" >&nbsp;&nbsp;& Nbsp;&nbsp;</code><code class= "Java plain" >}</code></div></div></td>< /tr></tbody>

The differences between Class.forName and ClassLoader load classes in Java reflection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.