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

How to understand and master Java inner classes

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "how to understand and master the inner class of Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

(1) Overview

If a class is defined inside another class, the class is called an inner class.

For example: class Inner is defined in class Outer, and class Inner is called inner class.

Class Outer {class Inner {}}

(2) access rules for inner classes A: members of external classes can be accessed directly, including private ones

B: for an external class to access members of an inner class, you must create an object

(3) Classification of internal classes

A: member inner class

B: local inner class

C: static inner class

D: anonymous inner class

(1) member inner class member inner class-that is, the characteristics of the class located in the member location of the external class: you can use all member variables and member methods in the external class (including private)

A: format:

Class Outer {private int age = 20; / / member location class Inner {public void show () {System.out.println (age);} class Test {public static void main (String [] ages) {/ / member inner class is a non-static demonstration that Outer.Inner oi = new Outer.new Inner (); oi.show ();}}

B: when creating an object: / / the inner class of the member is not static: external class name. Internal class name object name = new external class name.new internal class name ()

/ / the inner class of the member is static: the external class name. Internal class name object name = new external class name. Inner class name (); C: member inner class common modifier: A:private

If our inner class does not want to be easily accessed by anyone, we can choose to use private to modify the inner class, so we cannot access it through the method that creates the object, we just need to define a public-decorated method in the external class and call it indirectly. The advantage of this is that we can add some judgment statements to this public method to play the role of data security.

Class Outer {private class Inner {public void show () {System.out.println ("password backup file");}} public void method () {if (you are the administrator) {Inner I = new Inner (); i.show ();} else {System.out.println ("you do not have permission to access");}

Let's give a more standardized way to write it.

Class Outer {private class Inner {public void show () {System.out.println ("password backup file");}} / / use getXxx () to get the inner class of a member, and you can add a check statement (omitted in this article) public void getInner () {return new Inner ();} public static void main (String [] args) {Outer outer = new Outer (); Outer.Inner inner = outer.getInner (); inner.show ();}}

B:static, which is modified by static, belongs to the inner class of members by location, but it can also be called static inner class, and it is often called nested inner class. We will explain the details in detail below.

D: classic questions within members (fill in the blanks)

Please fill in the blanks in the parentheses after the three println to make the output 25,20,18.

Class Outer {public int age = 18; class Inner {public int age = 20; public viod showAge () {int age = 25; System.out.println (age); / / empty 1 System.out.println (this.age); / / empty 2 System.out.println (Outer.this.age); / / empty 3}

(2) Local inner class-that is, the characteristics of a class defined in a method or scope: the scope has changed and can only be used in its own methods and properties.

A format:

Class Outer {public void method () {class Inner {}

B: when visiting:

/ / in a local location, you can create an inner class object by calling the object and the inner class method class Outer {private int age = 20; public void method () {final int age2 = 30; class Inner {public void show () {System.out.println (age); / / accessing the variable age2 within the method from the inner class, which needs to be declared as the final type. System.out.println (age2);} Inner I = new Inner (); i.show ();}}

C: why do local inner classes have to be modified with final to access local variables?

Because the local variable is called with the call of the method, it disappears after use, and the data in the heap memory does not disappear immediately.

So, the heap memory still uses this variable, and the variable is gone. To keep the value alive, add a final modifier.

The reason is that when we use final to decorate variables, heap memory directly stores values, not variable names.

(that is, the location of the above example age2 stores the constant 30 instead of the variable name of age2)

(3) static inner class

We know that static cannot be used to modify a class, but the inner class of a member can be regarded as a member of an external class, so it can be decorated with static. This kind of inner class decorated with static is called static inner class, also known as nested inner class. Features: non-static member variables and member methods of external classes cannot be used

Explanation: when a non-static inner class is compiled, a reference to the external class is saved by default, while the static class does not.

Simple understanding:

Static inner class objects can be created even without external class objects, while non-static members of external classes must rely on object calls, while static members can directly use class calls without relying on external class objects, so static internal classes can only access static external properties and methods.

Class Outter {int age = 10; static age2 = 20; public Outter () {} static class Inner {public method () {System.out.println (age); / / error System.out.println (age2); / / correct}} public class Test {public static void main (String [] args) {Outter.Inner inner = new Outter.Inner (); inner.method ();}}

(4) Anonymous inner class an unnamed class, which is the abbreviated A format of the inner class:

New class name or interface name () {override method ();} essence: it is actually an anonymous object of a subclass that inherits this class or implements the interface

This is the reason why you can use new Inner () {} .show () directly in the following example = = subclass object .show ()

Interface Inter {public abstract void show ();} class Outer {public void method () {new Inner () {public void show () {System.out.println ("HelloWorld");}. Show ();}} class Test {public static void main (String [] args) {Outer o = new Outer (); o.method ();}

What if there are multiple methods in an anonymous inner class?

Inter I = new Inner () {/ / polymorphic, because new Inner () {} represents a subclass object of the interface, public void show () {System.out.println ("HelloWorld");}}

B: the use of anonymous inner classes in development

When we develop, we will see abstract classes, or interfaces as parameters.

At this point, what is really needed is a subclass object.

If the method is called only once, we can simplify the format of the anonymous inner class.

This is the end of the content of "how to understand and master Java inner classes". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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