Several special classes in Java---inner classes

Source: Internet
Author: User

The Inner class-----is the class in the other class

1. Definition of inner class

    • Define class as Class xxxx{...} Inside a class, you can '
    • Build Xxxx$xxxx class file at compile time for internal classes
    • Inner class cannot have the same name as an external class

2, the use of internal classes

    • Use this class in a class that encapsulates its class province, using the same method as the normal class
    • Use this class elsewhere: the class name of an outer class is preceded by a class name; the outer class name when you create an inner class object with new. Inner class name inner class object name = Outer class object name. New inner class name (parameter)

3. Using members of an external class in an inner class

    • The inner class can use all the fields and methods of the outer class directly; private can also
    • If the inner class has a field method with the same name as the outer class, the external class name. this. field or method

code example:

 Public classTestinnerclass { Public Static voidMain (string[] args) {externalclass extern=NewExternalclass (); Externalclass.innerclass Inner= extern.NewInnerclass (); Inner.output (The field of the method); }}classexternalclass{PrivateString s = "outer class field"; classinnerclass{PrivateString s = "Inner class field";  Public voidOutPut (String s) {System.out.println (The value of "s" is "+s); System.out.println (The value of "THIS.S" + This. s); System.out.println (The value of "EXTERNALCLASS.S" is "+ Externalclass". This. s); }    }    }
View Code

Output Result:

The value of S is the field of the method  This The value of . S is the Inner class field externalclass.s the value of the External class field
View Code

Several special classes in Java---inner classes

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.