[Go] Java class

Source: Internet
Author: User

Class class (in Java.lang package, Instances of the class Classrepresent classes and interfaces in a running javaapplication):
&NB sp;   in Java, each class has a corresponding class object. That is, when we write a class, after the compilation is complete, in the generated. class file, a class object is generated that represents the type information for this class
    three ways to get a class instance:
       (1) Use the object call GetClass () method to get the class instance of the object;
      (2) Use the class's static method, forname (), to get an instance (Staticclass forname (String className)   Returns the Classobject associated with the class or interface with the given Stringname.
      (3) use. class to get class instances, and for encapsulation classes of basic data types, you can also use the. Type to get a class instance of the corresponding base data type
    in the newinstance () call class The default constructor method Objectnewinstance () (can be used when the name of the class is not known, an instance of this class is common)  creates a new instance of the class represented by this classobject.
  

1PublicClass Classtest {
2Publicstatic void main (String [] args) throws exception{
Span style= "color: #008080;" > 3 String str1=abc 4 Class cls1=str1.getclass ();
5 Class cls2=string. Class
6 Class cls3=class.forname ( "java.lang.string ");
7 System. Out.println (CLS1==CLS2);
8 System. Out.println (CLS1==CLS3);
10}

The returned result is: True,true.
Explanation: A virtual machine produces only one byte code, which can be used to produce multiple instance objects.

Reflection

Reflection is the mapping of the various components in the Java class into the corresponding Java classes. For example, a Java class is represented by an object in a class class: member variables, methods, construction methods, packages, and so on, are represented by a Java class, like a car is a class, the engine in a car, a gearbox, etc. are also classes. The class class that represents the Java class displays a series of methods to obtain information about variables, methods, constructors, modifiers, packages, and so on, which are represented by instance objects of the corresponding classes, which are field, method, Contructor, package, and so on.

Each member of a class can be represented by an instance object of the corresponding reflection API class, and after these instance objects are obtained by invoking the methods of class classes, what is the use of these instance objects? How to use it? This is the point of learning and applying reflection.

Reflection Application of construction method

The Consturctor (constructor) class represents a constructor method in a class

@[email protected] Gets all the constructor methods for a class: for example: Constructor [] constructors = Class.forName ("java.lang.String"). GetConstructors () ;

@[email protected] Gets a constructor method: For example: Constructor Constructor = Class.forName ("java.lang.String"). GetConstructor ( Stringbuffer.class);

@[email protected] Create instance object: Usual Way: String Str=new string (new StringBuffer ("abc"));
Reflection mode: String Str= (String) constructor.newinstance (new StringBuffer ("abc"));


[Go] Java class

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.