"Java" Java internal class analysis

Source: Internet
Author: User
Tags class definition

Inner class:

The normal class is defined in the outermost of a. Java text file, that is, defining a class that has no elements outside of the {} scope, but An inner class is a class that is defined inside a class.

Why does Java provide such a way to define a class? Because if you define a class as usual, either the public class CLS, or the class CLS, you want to use these classes directly, but when you need to define a class for programming, you just want a class to be used for that class . If you do not want this class to be used by others, the inner class is used.

Java There are four types of internal classes:

       !--[Endif]-->

!--[if!supportlists]-->a) member inner class member Inner class

!--[if!supportlists]-->b) local inner class local inner class

!--[if!supportlists]-->c) anonymous inner class anonymous inner class

2. static inner class static inner class (Nested class Nested class )

Non-static inner class

a non-static inner class is a class that has no static keyword adornments

need to have interaction between the inner class and his external class when using

member Inner class:

The position of the inner class definition is in the member position of the class, that is, inside the outermost {} of the class

the member inner class guarantees that all members of the external class are allowed access to the inner class, including the private, including the static

       access: inner class

outside class new: To use an existing intrinsic class object to new P.new Inner ()

in the inner class, access his external class member: External class . this. member

Local inner class:

defines a class that is similar to a local variable in a member method

usage is the same as local variable, cannot be decorated with public etc.

can access final variables defined elsewhere in the method , not final

can access the members of the class for which the method resides

can only be used in the scope of the method, that is, the outer class cannot be used, and outside the outside class can not be used

Static Inner class:

A static inner class is a nested class

The inner class can only access static members of the outer class (this is different from the member inner Class) because static

Declared outside the outer class with an external class. Inner class

Outside the class new direct new external class. The inner class can be, because the object does not exist.

    

Anonymous inner class:

Inner class that does not contain a name

An object is created at the time of definition and is used only this time

Often used when a function is passed to a parameter

Defines an anonymous inner class that must be defined in the case of a parent class or interface.

The definition format is the new parent class or interface () {member};

Anonymous inner class because it is anonymous, so there is no construction method, want to implement the function of constructing method to construct code block {}

An anonymous inner class can only access final local variables, because it is actually a variant of a local inner class

  An anonymous inner class is created implicitly by invoking the constructor of the parent class, and if the parent constructor has parameters, it must be passed in () in parentheses (x) without the need for final  

Note:

The underscore indicates that there is still doubt.  

Java There are four types of internal classes:

1. non-static inner class

!--[if!supportlists]-->a) member inner class member Inner class

!--[if!supportlists]-->b) local inner class local inner class

!--[if!supportlists]-->c) anonymous inner class anonymous inner class

2. static inner class static inner class (Nested class Nested class )

Inner class:

an inner class is a class that is defined inside a class.

When programming, you need to define a class, but only if you want to use a class for that class.

Non-static inner class

a non-static inner class is a class that has no static keyword adornments

need to have interaction between the inner class and his external class when using

member Inner class:

The position of the inner class definition is in the member position of the class, that is, inside the outermost {} of the class

the member inner class guarantees that all members of the external class are allowed access to the inner class, including the private, including the static

Access outside the external class: external Class . Inner class

outside class new: To use an existing intrinsic class object to new P.new Inner ()

in the inner class, access his external class member: External class . this. member

Local inner class:

defines a class that is similar to a local variable in a member method

usage is the same as local variable, cannot be decorated with public etc.

can access final variables defined elsewhere in the method , not final

can access the members of the class for which the method resides

can only be used in the scope of the method.

"Java" Java internal class analysis

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.