Class. forname and classloader

Source: Internet
Author: User

Write a simple class first

package org.test.three;public class Book {public Book(){System.out.println("Here is Book construction.");}public static String bookName="mybook";private int price;private String author;private boolean isSale;static{System.out.println("Here is Book static area. The bookName is:"+bookName);}public void printAllInfo(){System.out.println("All info is[bookName:"+bookName+",author:"+author+",price:"+price+",isSale:"+isSale+"]");}}

Test class

Package org. Test. Three;/*** in Java, both Class. forname and classloader can be used to load classes. * In addition to loading the. Class file of the class to JVM, class. forname also interprets the class and executes the static block in the class. * The classloader only loads the. Class file to the JVM and does not execute static content. The static block is executed only in newinstance. ** The initialize Parameter Function in class. forname (name, initialize, loader) can also control whether to Load Static blocks. * Calling the newinstance () method will call the constructor to create class objects. ** @ Author lushuaiyin **/public class testmain {/*** @ Param ARGs */public static void main (string [] ARGs) {// test 0 // book bk0 = New Book (); // class. forname /////////////////////////////////////// /// try {//// Test 1 /// class cc0 = Class. forname ("org. test. three. book "); // Test 2 // class CC12 = Class. forname ("org. test. three. book "); // book BK1 = (book) cc12.newinstance (); // bk1.printallinfo ();/ ///// The same as above //// @ suppresswarnings ("unchecked") /// class <book> cc13 = (class <book>) class. forname ("org. test. three. book "); // book BK2 = cc13.newinstance (); // bk2.printallinfo (); // * Second Class. forname (name, initialize, loader) // name: name of the loaded class; whether initialize is initialized, that is, whether to execute the static code block and initialize the variable value. Loader: Class Loader // class. forname ("XXX") is equivalent to class. forname ("XXX", true, xxx. class. getclassloader (); // * // Test 3 // class CC14 = Class. forname ("org. test. three. book ", true, org. test. three. book. class. getclassloader (); // Test 4 /// class cc15 = Class. forname ("org. test. three. book ", false, org. test. three. book. class. getclassloader (); // Test 5 // class CC16 = Class. forname ("org. test. three. book ", true, org. test. three. Book. class. getclassloader (); // book bk3 = (book) cc16.newinstance (); // bk3.printallinfo (); ///// Test 6 // class cc17 = Class. forname ("org. test. three. book ", false, org. test. three. book. class. getclassloader (); // book bk4 = (book) cc17.newinstance (); // bk4.printallinfo (); //} catch (classnotfoundexception e) {// E. printstacktrace (); //} // catch (instantiationexception e) {// E. printstacktrace (); //} catch (illegalacc Essexception e) {// E. printstacktrace (); /////////////// //// // Test 7 // class cc21 = classloader. getsystemclassloader (). loadclass ("org. test. three. book "); // Test 8 // class cc22 = org. test. three. book. class. getclassloader (). loadclass ("org. test. three. book "); // test 9 // class cc23 = classloader. getsystemclassloader (). loadclass ("org. test. three. book "); // book bk5 = (B Ook) cc23.newinstance (); // bk5.printallinfo (); // test 10 // class cc24 = thread. currentthread (). getcontextclassloader (). loadclass ("org. test. three. book "); // book bk6 = (book) cc24.newinstance (); // bk6.printallinfo (); // TEST 11 class cc25 = thread. class. newinstance (). getcontextclassloader (). loadclass ("org. test. three. book "); book BK7 = (book) cc25.newinstance (); bk7.printallinfo ();} catch (classnotfoundexception e) {e. printsta Cktrace ();} catch (instantiationexception e) {e. printstacktrace ();} catch (illegalaccessexception e) {e. printstacktrace () ;}}/ * test 0 print here is book static area. the bookname is: mybook Test 1 print here is book static area. the bookname is: mybook Test 2 print here is book static area. the bookname is: mybookhere is book construction. all info is [bookname: mybook, Author: NULL, price: 0, issale: false] Test 3 print here is book static are A. the bookname is: mybook Test 4 print (no output content) Test 5 print here is book static area. the bookname is: mybookhere is book construction. all info is [bookname: mybook, Author: NULL, price: 0, issale: false] Test 6 print here is book static area. the bookname is: mybookhere is book construction. all info is [bookname: mybook, Author: NULL, price: 0, issale: false] Test 7 print (no output content) Test 8 print (no output content) test 9 print here is book static area. the bookname is: mybookhere I S book construction. all info is [bookname: mybook, Author: NULL, price: 0, issale: false] test 10 print here is book static area. the bookname is: mybookhere is book construction. all info is [bookname: mybook, Author: NULL, price: 0, issale: false] Test 11 print here is book static area. the bookname is: mybookhere is book construction. all info is [bookname: mybook, Author: NULL, price: 0, issale: false] // when you connect to a database, the following code is called, which is actually the loading Driver Class, execute stati C. register the driver. // Class. forname ("com. mySQL. JDBC. driver "); static {try {drivermanager. registerdriver (New Driver ();} catch (sqlexception e) {Throw new runtimeexception ("can't register driver! ") ;}} // Class. forname ("oracle. JDBC. driver. oracledriver "); static {defaultdriver = NULL; timestamp = timestamp. valueof ("00:00:00. 0 "); try {If (defaultdriver = NULL) {defadriver driver = new oracledriver (); drivermanager. registerdriver (defaultdriver) ;}} catch (runtimeexception) {} catch (sqlexception ){}}*/

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.