Internal class and anonymous internal class, anonymous

Source: Internet
Author: User

Internal class and anonymous internal class, anonymous

What is an internal class? $

Define another class within a class

Class A {int I; class B {// B is the internal class int j of A; int funB () {// int result =. this. I + this. j; equivalent to int result = I + j; return result ;}}}

After compiling a. java, in addition to generating A. class, there is also A $ B. class (class file generated by the internal class. External class $ internal class.Class). In B, you can use (not own) The member variables and member functions of A. B does not inherit.

Class Test {public static void main (String args []) {A a = new A (); // generate an internal class Object. B B =. new B ();. I = 2; B. j = 4; int result = B. funB (); System. out. println (result );}}

When generating an internal class object, you must first have an external class:

A a = new ();
A. B B = a. new B ();

 


What are the characteristics and functions of local and anonymous internal classes in JAVA? I 'd better explain it in detail.

Java internal class
There are four types: Member internal class, local internal class, static internal class, and anonymous internal class.
1. Member internal class: a member of an external class, which is in parallel with the attributes and methods of the external class.
Note: static variables cannot be defined in the member's internal class, but all members of the external class can be accessed.
Public class Outer {
Private static int I = 1;
Private int j = 10;
Private int k = 20;
Public static void outer_f1 (){
// Do more something
}
Public void out_f2 (){
// Do more something
}

// Member internal class
Class Inner {
// Static int inner_ I = 100; // static variables cannot be defined in the internal class
Int j = 100; // instance variables of internal and foreign departments can coexist.
Int inner_ I = 1;
Void inner_f1 (){
System. out. println (I); // If the variable of the external class does not have the same name as the variable of the internal class, you can directly use the variable name to access the variable of the external class.
System. out. println (j); // directly use the variable name to access the internal class's own variables in the internal class
System. out. println (this. j); // you can also use "this. variable name" in the internal class to access internal class variables.
// Access the instance variables with the same name as the internal class in the external class. You can use "external class name. this. variable name ".
System. out. println (k); // If the variable of the external class does not have the same name as the variable of the internal class, you can directly use the variable name to access the variable of the external class.
Outer_f1 ();
Outer_f2 ();
}
}
// Access the internal class of the member using non-static methods of the external class
Public void outer_f3 (){
Inner inner = new Inner ();
Inner. inner_f1 ();
}

// The static method of the external class accesses the internal class of the member, which is the same as the internal class of the external class access member.
Public static void outer_f4 (){
// Step 1 create an external Class Object
Outer out = new Outer ();
// *** Step 2: create an internal class object based on the external Class Object ***
Inner inner = out. new Inner ();
// Step 3: Access the internal class
Inner. inner_f1 ();
}

Public static void main (String [] args ){
Outer_f4 ();
}
}
Advantages of member Internal classes:
(1) The internal class acts as a member of the external class and can access the private members or attributes of the external class. (Even if the external class is declared as PRIVATE, it is still visible to internal classes .)
(2) Use an internal class to define inaccessible attributes of an external class. In this way, the external class has lower access permissions than the external class's private.
Note: Internal classes are a compilation concept. Once compiled successfully, they become completely different. For an external class named outer and its internal defined internal class named inner. After compilation is complete, the outer. class... remaining full text appears>

When are java internal classes and anonymous classes generally used? What are the benefits?

The internal class and Anonymous class have a feature that they can access all the members of the peripheral class, including the private

Therefore, you can use the internal class to provide some functional interfaces for external access to private members of the peripheral class.

This is usually used at the design level. It's not so common as to tell you when to use it as needed.

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.