Dynamic loading of class types and classes in Java

Source: Internet
Author: User

The following simplified code is available:

Preparation of Knowledge:

Student judy = new Student;

Class s1 = student.class;Class s2 = Judy.getclass ();

Class s3 = null;

s3 = class.forname ("com. Student ");Here s1, S2, S3 are all class type new object is static load class, at compile time need to load all the possible classes.

Examples of dynamic load classes:

1//Officeable.java
2Interfaceofficeable {
3
4 PublicvoidStart ();
5//System.out.println ("Word...stats
6}

1 // Word.java 2 class Implements officeable {34      Public void start () {5         System.out.println ("Word...starts ..."); 6     }78 }
1 //Officebetter.java2 classOfficebetter {3     4      Public Static voidMain (string[] args) {5     6         Try {7Class CType = Class.forName (Args[0]);//get class class type8Officeable OA =(officeable) ctype.newinstance ();9 Oa.start ();Ten  One}Catch(Exception e) { A e.printstacktrace (); -         } -     } the}

The Officebetter class can be executed from the command line, with the execution arguments given, the class name of the class to be loaded dynamically.

The above finishing from MU class network of Java teaching Video http://www.imooc.com/video/3725/0, in this thank Mu class nets teacher!

Dynamic loading of class types and classes in Java

Related Article

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.