Definitions and access rules for internal classes in Java

Source: Internet
Author: User

Java Internal class summary

In short, the inner class defines a class within the class that we are familiar with.

Why do I need an internal class?

When we describe things, there are things in them, and we use inner classes to describe things.

Because internal things are using the contents of external things

Let me give you an example. The human body has a heart, blood, liver, spleen, lung-...... So how does the heart define?

We should define the heart as the inner class, because it uses the outer class (human body) of the blood, oxygen ...

Example 1: The basic structure of an inner class

classOut/*External Class*/{    Private intX=3; classInch/*Inner class*/              {        Private inty Public voidShow () {System.out.println (x); }            }}classdemo{ Public Static voidMain (String args[]) {out.in in=NewOut ().NewIn ();/*to create the format of an inner class object*/in.show (); }}


Run Result:3

From the above example, it is not difficult to see that the inner class actually seriously destroys the good code structure, but why use the inner class?

Because inner classes are free to use member variables of external classes (including private ones) without generating objects of external classes, this is the only advantage of inner classes

Like the heart can directly access the body's blood, rather than through the doctor to draw the blood

After compiling the program, two. class files are generated, respectively Out.class and Out$in.class

Where $ represents the out.in in the above program.

Out.in in = new Out (). The new in () can be used to generate an object of the inner class, which has two small knowledge points to note

1. The first out is to indicate which outer class the inner class object needs to be generated

2. An object of an outer class must be preceded to generate an object of the inner class, because the inner class is intended to access member variables in the outer class

For other examples, see: http://www.cnblogs.com/nerxious/archive/2013/01/24/2875649.html

Definitions and access rules for internal classes in Java

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.