当前位置:网站首页>The difference between ordinary inner class and static inner class

The difference between ordinary inner class and static inner class

2022-04-23 18:56:00 Mug mop fan

summary

First, let's talk about the definition of internal classes : Inner class is to declare another class in a class , Static inner classes are used when declaring inner classes “static” keyword . The author understands the main differences between them are as follows :

1. Declaration method

To instantiate an ordinary inner class , First, you need to instantiate the external class where it is located , Then instantiate the inner class ; Static inner classes can directly declare .

2. Variable types that can be defined or accessed

<1> Ordinary inner classes cannot declare static Variable of type , Static inner classes can ;
<2> After the instantiation of an ordinary internal class, it holds the reference of an external class object , Inside the ordinary inner class, you can access the variables and methods in the outer class ; Static internal classes do not hold references to external class objects after instantiation , Variables and methods in external classes cannot be accessed in static internal classes .

3. How the external class accesses the internal class

Static methods in external classes cannot create ordinary internal class objects , You can create static inner class objects ; Static inner classes and ordinary inner classes can be approximated as static variables and ordinary variables , Static methods cannot access ordinary variables .

4. summary

To make a long story short , In terms of coupling , Ordinary inner classes are highly coupled with outer classes , The coupling between static inner classes and outer classes is very low . Approximately speaking , Static inner classes are defined inside a class , Almost no difference from external classes .

——————————————————————
This article is updated to my official account. : Scan or wechat search “ Tea migration ” Can focus on . The background to reply “ The difference between ordinary inner class and static inner class ” You can get the push of this article . I also send some articles related to programming in official account. , Welcome to your attention ~
 Tea migration

版权声明
本文为[Mug mop fan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210603050045.html