Java Internal classes

Source: Internet
Author: User

Static inner class static Inner class

The simplest form of inner class.

The static keyword is added to the class definition.

Cannot have the same name as an external class.

is compiled into a completely separate. class file with the name Outerclass$innerclass.class in the form.

  only static members and static methods of external classes can be accessed , including private static members and methods.

The static inner class object is generated in the following way:

Outerclass.innerclass inner = new Outerclass.innerclass ();

member Inner class member Inner class

The member inner class is also defined in another class, but is not defined with a static adornment.

A member inner class and a static inner class can be likened to non-static member variables and static member variables.

The member inner class is like an instance variable.

  It can access all the member variables and methods of its outer class, whether static or non-static .

Create an instance of the member's inner class inside the outer class:

This.new Innerclass ();

Create an instance of an inner class outside the outer class:

(New Outerclass ()). New Innerclass ();

To access members of an external class in an inner class:

Outerclass.this.member

Local internal class local Inner class

Local inner classes are defined in a method and are smaller than the range of methods. Is the least used type in an inner class.

Like local variables, it cannot be modified by public, protected, private, and static.

Only local variables of the final type defined in the method can be accessed.

Local inner classes are defined in methods, so they can only be used in methods, that is, you can only generate instances of local inner classes within a method and invoke their methods.

Anonymous inner class anonymous Inner class

The anonymous inner class is a local inner class without a name, not using the keyword class, extends, implements, and no constructor method.

An anonymous inner class implicitly inherits a parent class or implements an interface .

Anonymous inner classes are used more often as a method parameter.

In the generated. class file, the anonymous class generates the Outerclass$1.class file, which is based on the first few anonymous classes.

Java Internal 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.