Class name. This and the class name. class

Source: Internet
Author: User

1. When you use this in an inner class to refer to an object of the inner class, you can use the outer class name to access the Outer class object.

2, in Java, each class has a corresponding class object, when a class is written, after compiling, in the generated. class file, a class object is generated to represent the type information of the class. There are three ways to get a class instance:

1). Gets the class instance of the object by invoking the GetClass () method with the object
2). Use the static method of Class forname () to get a class instance by its name
3). Use the. Calss method to obtain a class instance, a wrapper class for the base data type, and a. Type to get a class instance of the corresponding base data type.

The following is the Testclass.java code:

 Public classTestClass { Public Static voidMain (string[] args) {//during runtime, if we want to produce an object of a class, the Java virtual Opportunity detects whether the class object of that type//has been loaded.         If not loaded, the Java virtual opportunity finds the. class file and loads it based on the name of the class. //when new Point () loads the class, the class is also loaded when the instance is constructed with forname. Load only onceSystem.out.println ("Before New Point ()"); NewPoint (); System.out.println ("After new Point ()"); Try{class.forname ("Line"); } Catch(Exception e) {e.printstacktrace (); }          //use the object call GetClass () method to get the class instance of the objectPoint pt =NewPoint (); Class C1=Pt.getclass (); System.out.println (C1.getname ()); //Result: point//use the static method of Class forname () to get a class instance by its name        Try{Class C2= Class.forName ("point"); System.out.println (C2.getname ()); //Result: pointPoint pp = (point) c2.newinstance ();//once a class object of a type has been loaded into memory, it can be used to produce all objects of that type. //newinstance () invokes the default constructor method in the class. Pp.output (); } Catch(Exception e) {e.printstacktrace (); }          //Get class instances (classes) using the. Class methodClass C3 = Point.class; System.out.println (C3.getname ()); //Result: point//use the. Calss method to obtain a class instance (base type)Class C4 =int.class; System.out.println (C4.getname ()); //Result: int//get class instances using. Class (encapsulation class for base data types)Class c5 =Integer.type; System.out.println (C5.getname ()); //Result: intClass C6 = Integer.class; System.out.println (C6.getname ()); //results: Java.lang.Integer    } }  classPoint {Static{System.out.println ("Loading Point"); }      voidoutput () {System.out.println ("x=" + x + ", y=" +y); }     intx, y;} classLine {Static{System.out.println ("Loading Line"); } } 

Class name. This and the class name. 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.