Java reflection mechanism and dynamic loading class Learning Essentials

Source: Internet
Author: User

Get the object of class:

Suppose Foo is a class, Foo foo = new Foo ();

The first type: Class c1 = Foo.class;

The second type: Class C2 = Foo.getclass ();

The third type: Class C3 = Class.forName ("Com.nudt.reflection.Foo"); Will throw an exception

At this time C1 = = C2 = = C3 is True


You can also create an instance of Foo through C1\C2\C3:

Foo foo = (foo) c1.newinstance (); Premise: Foo has a parameterless construction method


Dynamic load class (Runtime load Class):

Class C = Class.forName (class full name);

(Animal) A = (Animal) c.newinstance ();

Where animal is an interface, the class to be loaded at run time inherits the interface, and creates its own parameterless construction method


Get a Method object:

The reflection operation of the method:

Method.invoke ()


Generics in Java are protected from erroneous input, and no generics exist after compilation.

You can bypass the compilation by reflection of the method, speaking int into the list<string>

Java reflection mechanism and dynamic loading class Learning Essentials

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.