Class and usage in Java

Source: Internet
Author: User

Class and usage in Java

Java program at runtime, the Java runtime System has been all the objects of the so-called run-time type identification, known as the Rtti. This information records the classes each object belongs to. A virtual machine typically uses run-time type information to perform the correct method, and the class that holds the type information is the classes class. The class class encapsulates the state of an object and an interface runtime, and when the class is loaded, objects of class type are automatically created.

To be blunt:
    • Class is also a class, except that the name and class keyword are highly similar. Java is a case-sensitive language.
    • The object content of class is the type information of the class you create, for example, if you create a shapes class, then Java generates an object with the class class of shapes .
    • Objects of class class cannot be created as new shapes () like normal classes, whose objects can only be created by the JVM, because there is no public constructor for this class
    • The function of the class is to provide or obtain the type information of an object at run time, similar to the typeid () function in C + +. This information can also be used for reflection.

      1.Class class principle

      We all know that all Java classes inherit the class object, and there is a method in the object class: GetClass (). This method is used to obtain a reference to the class of an object that has been instantiated by this class, which refers to the object of class. We cannot generate a class object ourselves (the constructor is private), and the class object is created by the Java virtual machine to automatically create the class object when the classes are called, or through the DefineClass method in the class loader. The object we generate will have a field that records the location of the object to which the object belongs in the class class. As shown in the following:

2. Get a Class class object

The first approach, the forname function of class

public class shapes{}  Class obj= Class.forName("shapes");

The second approach, using the object's getclass () function

public class shapes{}shapes s1=new shapes();Class obj=s1.getClass();Class obj1=s1.getSuperclass();//这个函数作用是获取shapes类的父类的类型

Third approach, using class literal constants

Class obj=String.class;Class obj1=int.class;

Note that when you use this method to generate a class class object, the JVM will not automatically load the class (such as the String Class). The other option would cause the JVM to initialize the class.

3. Using the Class class object to generate an instance of the target class to generate an Imprecise object instance

Once you have a class object, you can use the newinstance () function to generate an instance of the target class. However, the function does not generate an instance of the target class directly, only an instance of the object class can be generated

Class obj=Class.forName("shapes");Object ShapesInstance=obj.newInstance();
To generate a target instance of a band type using a generalization class reference
Class<shapes> obj=shapes.class;shapes newShape=obj.newInstance();

Because of the type restrictions, object references that use generalization class syntax cannot point to other classes.

Class obj1=int.class;Class<Integer> obj2=int.class;obj1=double.class;//obj2=double.class; 这一行代码是非法的,obj2不能改指向别的类了

However, there is a flexible usage that allows you to use class objects to point to any subclass of the base class.

Class<? extends Number> obj=int.class;obj=Number.class;obj=double.class;

Therefore, the following syntax generates a class object that can point to any class.

Class<?> obj=int.class;obj=double.class;obj=shapes.class;

The last strange usage is that when you use this generic syntax to build a base class object for an object of the class class you have, you must use the following special syntax

class shapes{}class round extends shapes{}Class<round> rclass=round.class;Class<? super round> sclass= rclass.getSuperClass();//Class<shapes> sclass=rclass.getSuperClass();

We know that the base class of round is the shapes, but cannot directly declare class < shapes >, must use special syntax

Class <? Super Round >

Class and usage in Java

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.