Dynamic class loading for Java classes

Source: Internet
Author: User

All things are objects

Class is an object of Java.lang.Class

Package Com.mysec.reflex;
Import Java.util.Scanner;
Any class is an instance object of class
public class Officebeffer {

public static void Main (string[] args) {
Scanner sc = new Scanner (system.in);
String name = Sc.nextline ();
Methodstart (name);
}

public static void Methodstart (String name) {
try {

Know that objects of this class pass the GetClass method
Test C1 = new test ();
C1.getclass ();
Any class has an implied static member variable
class<?> C2 = Test.class;

Dynamically loading classes, loading at run time
Com.mysec.reflex.Word
class<?> C = class.forname (name);
//Create objects of this class by class type
officeable OA = (officeable) c.newinstance ();
Oa.start ();
} catch (Exception e) {
E.printstacktrace ();
}
}
}



Package Com.mysec.reflex;
Public interface Officeable {
public void start ();
}

Package Com.mysec.reflex;
public class Word implements officeable{
@Override
public void Start () {
System.out.println ("Wordstarts ...");
}
}

Dynamic class loading for Java classes

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.