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

What is the inner class of Java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "what is Java internal class", the content is simple and easy to understand, organized clearly, I hope to help you solve doubts, let Xiaobian lead you to study and learn "Java internal class" this article bar.

I. Introduction to Internal Classes

1. Definition: A class is nested inside a class. The nested class is the inner class. The nested class is called the outer class. An inner class is one of the five members of a class (properties, methods, constructors, code blocks, inner classes).

Category:

2. Member internal classes: 1. Non-static internal classes

Description: Defined in member position (outside method or code block), no static modifier.

1. Direct access to all members of an external class

2. You can add arbitrary permission modifiers

3. Scope, which applies to the entire outer class like any other member

package local_class;public class localclass //attribute location private int n1=10; class localN1{//local class public void output(){ //==1==. Direct access to all members of external classes, including private ones System.out.println("Get private member"+n1); } } //Write a method to create a real column of a local class public void output(){ localN1 LocalN1 = new localN1(); LocalN1.output(); }}

Achieve:

2. Static internal classes

** Definition in member position (outside method or code block), with or without static modifier.

1. Static members of external classes can be accessed directly, and non-static members cannot be accessed.

2. You can add arbitrary permission modifiers

3. Scope, which applies to the entire outer class like any other member

Code:

package local_class;public class _static_localclass private int n1=10; static int n2=20; static public class localN2{//static local class public void output(){ //==1==. Direct access to all members of external classes, including private ones System.out.println("Get_static_localclass private member"+n2); //System.out.println(n1); } } //Write a method to create a real column of a local class public void output(){ localN2 LocalN2 = new localN2(); LocalN2.output(); }}

Member internal class: usage is relatively simple.

Second, the local internal class:

Description: A local class is defined within a method or code block of an outer class.

1. All members of an external class can be accessed directly.

2. Access modifiers cannot be added, the status of a local inner class is a local variable (so it can be modified with fianl).

3. Scope: acts on defined methods or code blocks.

4. Access mode: Local inner class access external outer class members can directly access. An outer class accesses a local inner class, creating objects in the outer class scope before accessing them.

5. External classes cannot access internal classes.

6. When an external class member has the same name as an internal class member, the default is the principle of proximity. If you want to use an external class, you can use: external class name.this. member.

For example:

2. Famous local interior classes

error code

correct code

package _inner_class;public class Named_local_inner_class private int a1=1; int a2=2; private void fun(){ System.out.println("output private method"); } //first case, in the method //The second case, in the code block (I won't type the code as in the method) //Take the method as an example. public void output(){//method class inner_class{//Local inner class //1. All members of the outer class can be accessed public void _output(){ System.out.println("a1="+a1);//private member accessed System.out.println("a2="+a2); fun(); } } //Just instantiate and call the output() method. //Note scope inner_class Inner_class=new inner_class(); Inner_class._ output(); }//End method}//External class

call

Anonymous local interior classes (important)

The essence is still an internal class is a class, the class has no name (in fact, there is a name, but we can not see, system allocation).

2. It's also an object. basic syntax

new class name or interface name (parameter list){

class body

};

Call anonymous inner class:

output

Distinguish from well-known local interior classes

A22 objects are used only once, and anonymous local inner classes are used when they are no longer used.

What is the compilation type and run type of A22? (The runtype is actually an anonymous local inner class, which is XXXXX. The outer class name is $1)

As shown in the figure:

That's all for "What are Java inner classes?" Thanks for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

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