当前位置:网站首页>Overview of static inner classes and non static inner classes
Overview of static inner classes and non static inner classes
2022-07-27 12:39:00 【Rippling rippling】
distinguish Concept :
According to whether there is static Modifiers are divided into static inner classes and non static inner classes ,
Yes static Decorated as static inner class ,
No, static Decorated as a non static inner class
Static inner class
Statically named inner classes
cross-categorical If you use it Must be “ External class class name . Internal class name ”
This category If you use it need not Use the external class name
example :
Body class :
package com.jd;
/**
* External class
*/
public class Body {
/**
* Statically named inner classes
*/
public static class Heart{
public void beat(){
System.out.println(" It's beating ...");
}
}
}
Test class :
package com.jd;
import com.jd.Body.Heart;
public class Test {
public static void main(String[] args) {
Heart heart = new Body.Heart();
heart.beat();
}
}
Medium
Heart heart = new Body.Heart();
heart.beat();
Static inner classes can only direct Access to external classes Static member variables and methods ,
Can pass Create an object of an external class Use indirectly Non static member variables and methods .
public class OutClass {
private double weight=72;
public static void print(String name) {
System.out.println(name);
}
static class InClass{
{
double weight = new OutClass().weight;
// because weight It's not static , Therefore, when using in static inner classes, you must first create external class objects
print("Tom");
// because print Method is a static method , So you can use it directly .
}
}
}
Internal classes defined in non external classes are the same as local variables , Its use range starts from the defined position to the end of its direct statement block .
public class OutClass {
public static void main(String[] args) {
if(args!=null) {
class InClass{
}
}
InClass inClass = new InClass();
// Can't create object , Because the scope of the inner class cannot work here
}
}
Static members are only allowed in well-known static inner classes ( Static attribute 、 Static code blocks and static methods ).
Static anonymous inner class
Just add static that will do .
class Father {
public void eat() {
System.out.println(" Eat with chopsticks ....");
}
}
/**
* External class
*/
public class OutClass {
/**
* Anonymous inner class
*/
static Father son = new Father(){
@Override
public void eat() {
System.out.println(" Eat with chopsticks ....");
}
};
}
notes :
If static Inner classes can only be defined directly in outer classes . Not in the method , Block of code .
public class OutClass {
public static void main(String[] args) {
/**
* Statically named inner classes
*/
static class InClass {
public void printInfo() {
System.out.println(" I'm a famous inner class ");
}
}
}
}



Non static inner class
Non static inner classes defined directly in classes , Create objects in static methods , must “ Explicit ” Create an external class object
边栏推荐
- Wechat applet session holding
- I/o instance operation
- Chapter 10 enumeration classes and annotations
- 最强分布式锁工具:Redisson
- Advance in the flutter project_ image_ Picker component usage
- Plus版SBOM:流水线物料清单PBOM
- 20210519 leetcode double pointer
- Solve the problem of @onetomany query falling into circular reference
- Will MySQL fail to insert data? Why?
- Solution: can not issue executeupdate() or executelargeupdate() for selections
猜你喜欢

Julia beginner tutorial 2022

Necessary foundation: Signature Verification

Openpyxl drawing area map

Map接口

Bishi journey

评价自动化测试优劣的隐性指标

J9 number theory: how long is the mainstreaming of decentralized identity?

【数据库数据恢复】SQL Server数据库所在磁盘分区空间不足报错的数据恢复案例

Vi. analysis of makefile.build

Good looking (dynamic) Jay fan self-made dynamic album card (front and back are different) and lyrics page
随机推荐
CMD Chinese garbled code solution
详述HashSet的add方法
An overview of kernel compilation system
2021-3-22-directed graph sorting
最强分布式锁工具:Redisson
Application parameters of Southern biotech HRP labeled avidin avidin avidin
Common usage of curl command
Play CSDN editor
js真伪数组转换
Unity 2D game tutorial
Watermelon Book + pumpkin book chapter 1-2
Log4j2.xml configuration details
20210419 combined sum
JVM memory model
20210519 leetcode double pointer
Top 10 international NFT exchanges
J9 number theory: how long is the mainstreaming of decentralized identity?
The song of the virtual idol was originally generated in this way!
2021-3-22-tencent - minimum number of guards
Simple blockchain day based on bolt database (2)