Java Internal class summary

Source: Internet
Author: User

Internal class:

1, overview
An inner class is the definition of a class within an external class.
An inner class is a member of an outer class and is attached to an external class.
The inner class can be static and can be decorated with protected and private. (The outer class is not available: The outer class can only

Use public and default).

2, classification
member Inner class,
Local inner class,
Static Inner class,
Anonymous inner class form is to be used, must be mastered.

3. How to use

A, member inner class:
exists as a member of an external class and is tied to the properties and methods of the outer class.

public class Outer {
private static int i = 1;
private int j = 10;
private int k = 20;
public static void Outer_f1 () {

}
public void Outer_f2 () {

}

member inner class, static members cannot be defined
member Inner class, you can access all members of the external class
Class inner{
static int inner_i = 100;//not allowed in inner class
int j=100; instance variables of inner and outer classes can coexist
int inner_i = 1;
void Inner_f1 () {
System.out.println (i);
System.out.println (j);//Access inner class from inside class

The variable name is directly used for its own variables
System.out.println (THIS.J);/or use this. Variable name
System.out.println (OUTER.THIS.J);//In inner class

An external class name is used to access an instance variable with the same name as an inner class in an external class. this. Variable name
System.out.println (k);//If there is no external in the inner class

class, you can access the external class variable directly with the variable name
Outer_f1 ();
Outer_f2 ();

}
}
Non-static method of external class access member inner class
public void Outer_f3 () {
Inner Inner = new Inner ();
Inner.inner_f1 ();
}

The static method of an external class accesses a member's inner class, and an external class that accesses a member

Sample
public static void Outer_f4 () {
Step1 set up external class objects
Outer out = new Outer ();
Step2 an internal class object based on an external class object
Inner Inner = Out.new Inner ();
Step3 ways to access internal classes
Inner.inner_f1 ();
}

public static void Main (string[] args) {
Outer_f4 ();
}

}

Benefits of members ' inner classes:
⑴ internal classes as members of external classes, you can access private members or properties of external classes. (Even if the external class sound

Ming is private, but it is visible for internal classes within it. )
⑵ defines an inaccessible property in an external class with an inner class. This allows you to implement an external class that is more

Private also have a small access rights.
Note: The inner class is a compile-time concept that, once compiled successfully, becomes a completely different class.
For an external class named Outer and its internally defined internal class named inner. Occurs after compilation completes

Two types of Outer.class and Outer$inner.class.

Note: When outer is a private class, the external class is private to its external access, so you cannot

An external class object is established, which in turn fails to establish an internal class object.

</

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.