A simple example of static loading and dynamic loading of Java reflection _java

Source: Internet
Author: User
Tags reflection

Static loading:

Package COM.IMOOC. Loading class;

public class Office_static {public
	static void Main (string[] args) {
		//new Create object, is statically loaded class, need to load all possible classes at compile time
		if ("word". Equals (Args[0])) {
			Word w = new word ();
			W.start ();
		}
		if ("Excel". Equals (Args[0])) {
			Excel e = new Excel ();
			E.start ();}}

This program must be compiled with Word and Excel two classes exist to do, even if you do not use Excel to load

Dynamic loading:

1, Interface officeable:

Package COM.IMOOC. Loading class;

Public interface Officeable {public
	void Start ();
}

2, Word implementation interface:

Package COM.IMOOC. Loading class;

public class Word implements officeable{public
	void Start () {
		System.out.println ("Word start");
	}


3, Excel Implementation interface:

Package COM.IMOOC. Loading class;

public class Excel implements officeable{public

	void Start () {
		System.out.println ("Excel start");
	}

4. Main method

Package COM.IMOOC. Loading class;

public class Officebetter {
	/**
	 * @param args
	 *
	/public static void main (string[] args) {
		try {
			/ /dynamic load class, at run time loaded class
			C = Class.forName (Args[0]);//Enter COM.IMOOC in Run configuration. Load class. Excel
			//Through the class type, create the class object ( First convert to Word and Excel's Common interface officeable)
			officeable oa = (officeable) c.newinstance ();
			Oa.start ();
			
			The following two are not recommended because you are not sure whether to load Word or Excel, or to turn
//			Word word = (word) c.newinstance ();			Word.start ();			Excel Excel = (Excel) c.newinstance ();			Excel.start ();
		} catch (Exception e) {
			e.printstacktrace ();}}}


The above is a small series for everyone to bring the static load of Java reflection and dynamic loading of the simple example of all the content, I hope to help you, a lot of support cloud Habitat Community ~

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.