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

The usage of inner classes in Java

2025-01-18 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 inner classes in Java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "the use of internal classes in Java"!

A little bit of eye contact

The use of inner classes is discussed in three cases:

1 use inner classes inside external classes

2 use non-static inner classes outside of external classes

3 use static inner classes outside of external classes

Second, use inner classes inside external classes.

Variables can be defined directly through the class name of the inner class, and instances can be created by calling the internal constructor through new.

Do not use non-static inner classes in static members of external classes, including static methods and static initialization blocks, because static members cannot access non-static members.

Third, use non-static inner classes outside external classes

1 finishing touch

Private-decorated inner classes can only be used inside external classes. So to use a non-static inner class outside the outer class, you must use other modifiers.

Inner classes that omit access control characters: can only be accessed by other classes that are in the same package as the external class.

Inner classes decorated with protected: can be accessed by other classes and subclasses of external classes in the same package as the external class.

Inner classes decorated with public: can be accessed anywhere.

Define internal classes (both static and non-static) outside the outer class in the following syntax format:

OuterClass.InnerClass varName

The full inner class name should be OuterClass.InnerClass. If the external class has a package name, you should add the package name prefix

The syntax for creating an object using a non-static inner class outside the external class is as follows:

OuterInstance.new InnerConstructor ()

2 code

CreateInnerInstance.java

Class Out {/ / defines an inner class without using access control characters. / / that is, only other classes in the same package can access the inner class class In {public In (String msg) {System.out.println (msg);} public class CreateInnerInstance {public static void main (String [] args) {Out.In in = new Out (). New In ("Test Information") / * the above code can be changed to the following three lines of code: create an external class instance by defining the internal class variable Out.In in; in the form of OutterClass.InnerClass, and the non-static internal class instance will be hosted in the instance Out out = new Out (); call the internal class constructor through the external class instance and new to create a non-static internal class instance in = out.new In ("test information"); * /}}

3 run

Test information

4 code

SubClass.java

Public class SubClass extends Out.In {/ / shows the constructor public SubClass (Out out) that defines the SubClass {/ / explicitly calls In's constructor out.super ("hello") through the incoming Out object;}}

CreateInnerInstance.java

Class Out {/ / defines an inner class without using access control characters. / / that is, only other classes in the same package can access the inner class class In {public In (String msg) {System.out.println (msg);} public class CreateInnerInstance {public static void main (String [] args) {Out.In in = new Out (). New In ("Test Information") / * the above code can be changed to the following three lines of code: create an external class instance by defining the inner class variable Out.In in; in the form of OutterClass.InnerClass, and the non-static internal class instance will be stored in the instance Out out = new Out (); call the internal class constructor through the external class instance and new to create a non-static internal class instance in = out.new In ("test information"); * / SubClass subclass = new SubClass (new Out ()) }}

5 run

Test Information hello

6 description

Both the non-static inner class In object and the SubClass object must hold a reference to the Outer object. The difference is that the Out object is passed in when creating the two objects: when creating an object of the non-static inner class In class, the new keyword must be called through the Outer object, and when creating an object of the SubClass class, the Outer object must be used as the caller to call the constructor of the In class.

The subclass of a non-static inner class is not necessarily an inner class, it can be an external class. However, a subclass instance of a non-static inner class also needs to retain a reference to the object of the external class in which its parent class resides. That is, if an object of an inner class subclass exists, there must be an external class object corresponding to it.

Fourth, use static inner classes outside of external classes

1 finishing touch

The syntax for creating objects using static inner classes outside the external class is as follows:

New OuterClass.InnerConstructer ()

2 code

Class StaticOut {/ / defines a static inner class without using access control characters. / / that is, other classes in the same package can access the inner class static class StaticIn {public StaticIn () {System.out.println ("the constructor of the static inner class");} public class CreateStaticInnerInstance {public static void main (String [] args) {StaticOut.StaticIn in = new StaticOut.StaticIn () / * the above code can be changed to the following two lines of code: define the inner class variable StaticOut.StaticIn in; in the form of OutterClass.InnerClass and call the internal class constructor through new to create a static inner class instance in = new StaticOut.StaticIn (); * /}}

3 run

Constructor of static inner class

At this point, I believe you have a deeper understanding of "the use of internal classes in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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