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

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

Share

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

This article mainly introduces "what is an internal class". In daily operation, I believe many people have doubts about what is an internal class. I have consulted all kinds of materials and sorted out simple and easy operation methods. I hope it will help you answer the doubts about "what is an internal class"! Next, please follow the small series to learn together!

content of this article

What is an internal class?

If we define a class A inside another class B, then class A is called an inner class.

Why do we need inner classes?

There are three reasons:

Inner class methods can access data in the scope of the class definition, including private data.

Inner classes can be hidden from other classes in the same package.

Anonymous inner classes are handy when you want to define a callback function and don't want to write a lot of code.

What are the characteristics of inner classes?

An inner class can have multiple instances, each with its own state information, independent of that of other peripheral objects.

Within a single outer class, multiple inner classes can implement the same interface in different ways, or inherit from the same class.

The moment at which an inner class object is created does not depend on the creation of a peripheral class object.

The inner class has no confusing "is-a" relationship; it is an independent entity.

Inner classes provide better encapsulation and are inaccessible to all classes except the outer class.

Think in Java

What are the types of internal classes?

Member internal class: one level with member variables

Local inner classes: inner classes in methods

Anonymous inner classes: implementation classes that basically belong to interfaces, one-time use scenarios.

static inner class: static-modified member inner class

How do I use inner classes?

The general form for creating static inner class objects is:

External class name. Inner class name xxx = new Outer class name. Internal class name ()

The general form of creating a member internal class object is:

External class name. inner class name xxx = outer class object name.new inner class name ()

The above is a simple answer. If the interview may continue to ask questions, you have to continue to read.

This is a golden section, and the above part is a beginner interview question.

Interview Answer:

Common Advanced Interview Questions

Why does a member inner class have unconditional access to members of an outer class?

Solution idea: first write a case to prove whether it is correct, and then deeply understand the relationship between the internal class and the external class of the member.

public class OuterClass { class InnerClass{ } }

InnerClass is the member inner class. Use javac to compile OuterClass, you can also compile it in IDEA or eclipse, after compilation you will find that there are actually two.class files.

Outer class: OuterClass.class

Member inner class: OuterClass$InnerClass.class

Use the javap command to view these two class files:

OuterClass.class contains the following:

It's a simple class, nothing different. Let's focus on whether the content of the.class file of the inner class is something special.

OuterClass$InnerClass.class reads as follows:

From the above we can see four points:

1, defines a constant that points to the address of an external class object.

2. The inner class has a constructor by default. If there are parameters, the parameters are external classes.

3, assign member variable 1(this$0) to the external class object address.

4, there are two member variables, one is itself, the other is a variable pointing to the address of the external class object.

So here's why inner classes can access members of outer classes (static inner classes are those that don't depend on outer classes, that is, objects of inner classes can be created without creating objects of outer classes).

It can also be concluded that the member inner class depends on the outer class, and the outer class must create a good object before the inner class can create an object.

At this point, the study of "what is an internal class" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

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