我们首先看看内置类和静态内枝类的区别,其实我们光从这个名字还不太容易高明白,下面是截取的网上的,理解起来还容易一点。
从字面上看,一个被称为静态嵌套类,一个被称为内部类。从字面的角度解释是这样的:什么是嵌套?嵌套就是我跟你没关系,自己可以完全独立存在,但是我就想借你的壳用一下,来隐藏一下我自己。什么是内部?内部就是我是你的一部分,我了解你,我知道你的全部,没有你就没有我。(所以内部类对象是以外部类对象存在为前提的)
链接:https://www.zhihu.com/question/28197253/answer/39814613
其实上面说的主要区别:就是在创建内部类实例的时候,下面的代码可以看出:
package com.example.test;
public class Test190 {
class inner{
public void show() {
System.out.println("这是内部类");
}
}
static class staticinner{
public void show1() {
System.out.println("这是静态内部类");
}
}
public static void main(String[] args) {
Test190 t = new Test190();
inner i = t. new inner();
staticinner si =new staticinner();
i.show();
si.show1();
}
}