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 in Java

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

Share

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

This article is about what inner classes are in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The so-called inner class is to declare another class inside the class, because only the current class can access the inner class, so the inner class is relatively safe. In addition, the inner class can inherit and implement other classes and interfaces. So multiple internal classes can be used to implement multi-inheritance in disguise (in fact, interfaces can simulate multi-inheritance). In addition, the function of closure can be realized with inner classes.

Inner classes probably include ordinary inner classes, static inner classes (also known as nested classes), and anonymous inner classes. Here are a few examples

CommoneInnerClass is a common inner class, InnerClass is a static inner class, and the access method is created for both in the main method.

Java code

Package com.ec.test.innerclass Public class OuterClass {public static class InnerClass {} public class CommoneInnerClass {} / * @ param args * / public static void main (String [] args) {/ / TODO Auto-generated method stub / / static inner class Create OuterClass.InnerClass ob=new OuterClass.InnerClass () / / static inner class creation can be omitted as follows and static method call like InnerClass ob1=new InnerClass (); / / external class instance OuterClass.CommoneInnerClass obj3=new OuterClass (). New CommoneInnerClass () is required for ordinary inner class creation. / / creation of anonymous inner class anonymous inner class has no name, only a pair of curly braces Thread t=new Thread () {public void start () {System.out.println ("Hello");}; t.start () / / Inner classes can simulate multiple inheritance, implement functions like closures, and are more secure, because only external classes can access the inner class} public void test () {new CommoneInnerClass ();}}

Anonymous inner class, can simulate closure through anonymous inner class, etc.

Java code

/ / creation of anonymous inner class Anonymous inner class has no name but a pair of curly braces Thread t=new Thread () {public void start () {System.out.println ("Hello");}}

The difference between static inner classes and general inner classes, static inner classes can have static properties and methods. But the ordinary one can't.

When using anonymous inner classes, keep the following principles in mind:

Anonymous inner classes cannot have constructors.

Anonymous inner classes cannot define any static members, methods, and classes.

Anonymous inner class cannot be public,protected,private,static.

Only one instance of an anonymous inner class can be created.

An anonymous inner class must be behind new, implicitly implementing an interface or implementing a class.

Because the anonymous inner class is a local inner class, all restrictions on the local inner class take effect.

This in anonymous classes and inner classes:

Sometimes, we use inner classes and anonymous classes. When using this in an anonymous class, the this refers to the anonymous class or the inner class itself.

At this point, if we want to use the methods and variables of the external class, we should add the class name of the external class.

Thank you for reading! This is the end of this article on "what is the inner class in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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