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 use Java inner class, member inner class, static inner class and method inner class

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use Java inner class, member inner class, static inner class and method inner class". The content in the article is simple and clear, and it is easy to learn and understand. please follow the editor's train of thought to study and learn "how to use Java inner class, member inner class, static inner class and method inner class".

I. Internal class

(a) what is the internal class?

You can redefine the class in the class, which is called the inner class, and the class in which the inner class is located is called the outer class.

(2) Internal class functions

1. The inner class provides better encapsulation, hiding the inner class within the outer class and not allowing other classes in the same package to access the class.

two。 Members of the inner class can directly access the private data of the external class because the inner class is treated as a member of the external class. However, the external class cannot access the implementation details of the inner class (such as the member variables of the inner class).

II. Internal classes of members

1. What is a member inner class

In addition to defining member variables and member methods in a class, you can also define a class called a member inner class. The member inner class can access all members of the external class.

two。 The external class accesses the inner class. First, create the external class object to create the inner class object and create the syntax format of the inner class object.

External class name. Internal class name variable name = new external class name {}. New internal class name {}

3. How to define a member internal class case

For example:

Class A {/ / defines the member variable private String name= "Zhang San" of the class; private int age=28; / / defines a member method to access the inner class public void test () {B b=new B (); b.say () } / / define a member inner class class B {public void say () {/ / the member inner class method accesses the member variable System.out.println of the outer class ("Hello! My name is "+ name+" this year "+ age+"!);} public class p17 {public static void main (String [] args) {An a=new A (); / / create external class a.test (); / / call test () method}}

The result of the output is:

Hello! My name is Zhang San. I'm 28 years old!

An is an external class, in which an inner class B and a test () member method are defined. In class B, a say () method is used to access the member variables name and age of the external class, create an instance object of the inner class B in the test () method, and call the say () method. If you want the external class to access the inner class, first create the external class object to create the inner class object. The code for creating the inner class object is as follows:

A.B a=new A () .new B (); / / create inner class a.say (); / / call the test () method

Third, static inner class

1. What is a static inner class

An inner class is called a static inner class if it is declared using the static keyword. It can be instantiated without creating external class objects.

two。 Create a static inner class syntax format

External class name. Internal class name variable name = new external class name. Internal class name ()

3. How to use static internal cases

For example:

Class A {/ / defines the member variable private static String name= "Li Hua" of the class; private static int age=18; / / defines the static inner class static class B {void say () {System.out.println ("Hello! My name is "+ name+" this year "+ age+"!);} public class p18 {public static void main (String [] args) {/ / TODO Auto-generated method stub A.B a=new A.B (); / / create inner class a.say (); / / call methods of inner class}}

The result of the output is:

Hello! My name is Li Hua. I'm 18 years old.

From the above code, inner class B is declared using the static keyword to represent a static inner class. Only static variables of an external class can be accessed in a static inner class. If the member variables that access the external class must be accessed through an instance of the external class, there will be no references to the external class inside the static!

Fourth, the method inner class

1. What is a method inner class

The inner class is defined in the method of the external class, and the inner class of the method can only be seen inside the method, that is, it can only be used within this method.

two。 The inner class of a method cannot be used outside the outer class method, and the method inner class cannot use access control characters and static modifiers.

3. How to use method inner classes

For example:

Class A {/ / defines the member variable private String name= "Li Hua" of the class; private int score=88; public void test () {/ / defines the inner class class B {void show () {/ / access the member variable System.out.println of the outer class in the method ("name:" + name+ "" + "English score:" + score+ "!");} B b=new B () / / create inner class object b.show (); / / call inner class method}} public class p19 {public static void main (String [] args) {/ / TODO Auto-generated method stub An a=new A (); / / create external class object a.test (); / / call test () method}}

The result of the output is:

Name: Li Hua English score: 88!

From the above code, an inner class B is defined in the test () method of class A, because the inner class B is the inner class of the method, the program can only create an instance object of the class in the method and then call the show () method, and the inner class of the method is a member variable that can access the external class.

Thank you for your reading, the above is "how to use Java inner class, member inner class, static inner class and method inner class". After the study of this article, I believe you have a deeper understanding of how to use Java inner class, member inner class, static inner class and method inner class. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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