Understand the. this and. new syntaxes of java-8.3 internal classes from the beginning

Source: Internet
Author: User

Understand the. this and. new syntaxes of java-8.3 internal classes from the beginning

In this section, we will talk about the. this and. new syntaxes of internal classes.

In fact, the above is a bit of a problem. The correct statement is the external Class Object. this and the external Class Object. new.

Example:

 

Package com. ray. ch03; public class Test {private int testId = 1; public Test () {System. out. println (testId);} class MyTest {private int myTestId = 0; public MyTest () {this. myTestId = Test. this. testId + 1; System. out. println (myTestId) ;}} public static void main (String [] args) {Test test = new Test (); Test. myTest myTest = test. new MyTest ();}}

Because the internal class already holds the reference of the external class by default, you can use the external class. this to reference the object, so there will be Test. this. testId + 1

 

Similarly, we will hold the reference of the object test, and then generate the internal class object through the. new syntax, so there is a new Test (). new MyTest () syntax.

 

To sum up, this chapter mainly discusses the. this and. new syntaxes of internal classes.

 

 

 

Related Article

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.