Three ways to get a class object

Source: Internet
Author: User

1: Obtained through the method getclass that each object has. Cons: You must create the class object before you can call the GetClass method.

2: Each data type (basic data type and reference data type) has a static property class. Cons: This class must be identified first.

The first two methods are not conducive to the extension of the program, because it is necessary to use a specific class to complete the program.

3: The method used in class, the static Forname method .

Specifies what class name to get what class bytecode file object, this way is strongest, as long as the class name of the string passed in.

//1. obtained for class loading based on the given class name

String classname = "Cn.itcast.reflect.Person";//From configuration file

Class clazz = class.forname (classname);//This object represents Person.class

//2. If you get the object, don't know what type is used to get the object type

Object obj = new person ();

Class clazz1 = Obj.getclass ();//Get the object specific type

//3. If you are explicitly getting a class object that is used primarily to pass a reference

Class clazz2 = Person.class;

Three ways to get a class object

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.