Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Knowledge summary of internal classes in Java

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

The main content of this article is "Java internal class knowledge summary", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "Java internal class knowledge summary" bar!

Java inner class

I. meaning

In the Java programming language, programs are built from class. A class can also be declared inside a class, which we call an inner class.

Second, function

With better encapsulation, we know that the access modifier for a normal class (non-inner class) cannot be private or protected, while an inner class can. When we declare the inner class as private, only the outer class can access the inner class, which well hides the inner class.

Inner classes can inherit (extends) or implement (implements) other classes or interfaces without being affected by external classes.

Inner classes can directly access the fields and methods of external classes, even if they are decorated with private, whereas external classes cannot directly access members of inner classes.

Third, principle

The inner class is a compile-time concept that generates two separate class files after compilation, as follows:

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

The compiled file is shown below:

After compilation, Outer.Inner is renamed to Outer$Inner, period (.) Was replaced with a dollar sign ($).

IV. Classification

Java inner class can be divided into member inner class, local inner class, anonymous inner class and static inner class.

1) member inner class

The inner class of a member can be treated as a member of an external class, and static members cannot be declared in the inner class of a member, but the static final field is an exception. We know that when a class is loaded, static members are initialized first. if the inner class of the member has static members, then the inner class will be generated before the external class, and the inner class serves the external class. the generation of the inner class before the external class may be out of control. When instantiating a member's inner class, the member's inner class holds a reference to the current object of the external class, so that members of the external class can be accessed directly in the member's inner class, even if it is private-decorated.

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

Running result:

Inner

Outer

Outer

The reference to the external class object of the inner class of a member is formed by adding the name of the external class before the this. This form is called

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

2) Local inner class

The use of the local inner class is basically the same as that of the member inner class, except that the local inner class is defined in the method of the external class, just like the local variable, it is not a member of the external class. The local inner class is not accessible outside the method, but its instance can be returned from the method, and the instance will exist until it is no longer referenced. Local inner classes can also access local variables and method parameters of their methods. 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) {/ / method parameter is final type 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 inner class

You can think of an anonymous inner class as a local inner class without a class name. Anonymous inner classes have the following characteristics:

1. An anonymous inner class cannot have a constructor. An anonymous inner class does not have a class name, so it is certainly impossible to declare a constructor.

2. An anonymous inner class must inherit or implement an interface. The type assigned to new is the supertype of the anonymous class. Anonymous classes cannot have displayed extends or implements clauses or have any modifiers.

3. Anonymous inner classes, like member inner classes and local inner classes, cannot declare static members.

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 inner class

Static inner class, which is also called nested class in some books, needs to be declared with static modifier. Static inner class is different from the first three inner classes. Static inner class does not hold references to the current object of external class, so it is impossible to access non-static members of external class in static internal 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); cannot access the non-static member out.println (id) of the external class } 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 inner class directly through the class name}} so far, I believe you have a deeper understanding of the "summary of Java internal class knowledge". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report