Java internal class summary, Java class Summary

Source: Internet
Author: User

Java internal class summary, Java class Summary

Java internal class

I. Meaning

In Java programming language, a program is constructed by class. You can declare a class within a class.

The class is called an internal class.

Ii. Functions

  • Better encapsulation is implemented. We know that the access modifier of a common class (non-internal class) cannot be private or protected, but the internal class can. When we declare the internal class as private, only the external class can access the internal class, which hides the internal class.
  • Internal classes can inherit (extends) or implement (implements) other classes or interfaces without being influenced by external classes.
  • The internal class can directly access the fields and methods of the external class. Even if it is modified using private, the external class cannot directly access the members of the Internal class.

Iii. Principles

An internal class is a compilation concept. After compilation, two independent class files are generated, as shown below:

  public class Outer{      private String outerName = "outer";      class Inner{          private String innerName = "inner";      }  }

 

Compiled files include:

 

After compilation, Outer. Inner is renamed as Outer $ Inner, and the period (.) is replaced with the dollar sign ($ ).

Iv. Classification

Java internal classes can be divided into member Internal classes, local internal classes, anonymous internal classes, and static internal classes.

1) member internal class

The internal class of a member can be regarded as a member of an external class. static members cannot be declared in the internal class of the member, but static

The final field is an exception. We know that when a class is loaded, static members are initialized first.

The internal class is generated before the external class, while the internal class is for the external class, the internal class is

Generation may be out of control. When instantiating a member's internal class, the member's internal class will hold an external class's current

In this way, you can directly access the members of the External class in the internal class of the member, even if it is modified by private.

Import static java. lang. system. out; public class Outer {private String outerName = "outer"; // The external class cannot directly access members of the internal class. You need to instantiate the internal class Object private Inner inner = new Inner (); public class Inner {private String innerName = "inner"; public void show () {out. println (outerName); // members of the external class can be directly accessed} public void show () {out. println (inner. innerName); inner. show ();} public static void main (String [] args) {Outer outer = new Outer (); outer. show (); // instantiate the internal class Outer. inner inner = outer. new Inner (); inner. show ();}}

Running result:

Inner

Outer

Outer

The internal class of a Member refers to the reference of the external class object, which is composed of the external class name before this. this form is called

Limitation-this, out. println (outerName) is equivalent to out. println (Outer. this. outerName.

2) Local internal class

The use of the local internal class is basically the same as that of the member internal class, except that the local internal class is defined in the external class method

Like a local variable, it is not a member of an external class. Some internal classes cannot be accessed outside the method, but its instance can

And the instance will exist until it is no longer referenced. Local internal classes can also access

Local variables, method parameters, etc. The restriction is that local variables or method parameters can only be accessed when declared as final.

Import static java. lang. system. out; public class Outer {private String outerName = "outer"; public void show (final String str) {// The method parameter is final class Inner {public void print () {out. println (outerName + str) ;}} Inner inner = new Inner (); inner. print ();} public static void main (String [] args) {Outer outer = new Outer (); outer. show (": lalala ");}}

Running result:

Outer: lalala

3) Anonymous internal class

You can think of an anonymous internal class as a local internal class without a class name. An anonymous internal class has the following features:

1. No constructor is available for anonymous internal classes, and no class name is available for anonymous internal classes. Therefore, the constructor cannot be declared.

2. The anonymous internal class must inherit or implement an interface, and the new type is the super type of the anonymous class. The Anonymous class is not

There can be displayed extends or implements clauses, and there cannot be any modifiers.

3. Anonymous internal classes are the same as member Internal classes and partial internal classes, and static members cannot be declared.

Import static java. lang. system. out; public class Outer {private String outerName = "outer"; public void show (final String str) {new Inner () {// implements the Inner interface public void print () {out. println (outerName + str );}}. print ();} public static void main (String [] args) {Outer outer = new Outer (); outer. show (": lalala") ;}} interface Inner {void print ();}

Running result:

Outer: lalala

4) static internal class

Static internal class, also known as nested class in some books, must be declared with a static modifier, static internal class is different from the first three

The static internal class does not hold the reference of the current object of the external class, so the static internal class cannot access the external

The static internal class does not depend on the external class.

Import static java. lang. system. out; public class Outer {private String outerName = "outer"; private static int id = 123; private Inner inner = new Inner (); public static class Inner {public void print1 () {// out. println (outerName); non-static member out of the external class cannot be accessed. println (id);} public static void print2 () {out. println (id) ;}} public void show () {inner. print1 ();} public static void main (String [] args) {Outer outer = new Outer (); outer. show (); Outer. inner. print2 (); // access the static internal class directly through the class name }}

 

 

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.