需要加修饰符static;
    可以定义静态成员和非静态成员;
    不能直接访问包围类的非静态成员,可直接访问包围类的静态成员;
    可以通过包围类的对象进行访问非静态成员;
    外界可以通过静态嵌套类名访问其静态成员,通过对象访问其非静态成员;
    外界需要通过包围类才可以访问到静态嵌套类,并创建其对象,不需要外部包围类的实例;
    和正常的类没有什么区别,纯粹只是为了打包的便利性存在;

    1. public class Outer1 {
    2. String name;
    3. //静态嵌套类
    4. static class Inner1{
    5. String name;
    6. }
    7. }
    8. ---测试类
    9. public class MyTest1 {
    10. public static void main(String[] args) {
    11. Outer1.Inner1 obj = new Outer1.Inner1();
    12. //第三方需要通过外部包围类才可以访问到静态嵌套类
    13. }
    14. }

    静态内部类分析

    1. ----测试类
    2. public class Outer2Test {
    3. public static void main(String[] args) {
    4. //第三方类访问静态嵌套类
    5. Outer2.Inner1 obj1 = new Outer2.Inner1();
    6. obj1.getInnFlied1();
    7. Outer2.Inner1 obj2 = new Outer2.Inner1();
    8. System.out.println(obj1 == obj2);
    9. System.out.println("===================");
    10. Outer2 obj3 = new Outer2();
    11. obj3.outFun2();
    12. /*false
    13. ===================
    14. false
    15. inner 333
    16. inner hello
    17. inner static 444*/
    18. }
    19. }
    1. ----静态内部类
    2. public class Outer2 {
    3. private static String outStaticFiled2 = "outer static 222";
    4. private String outFiled = "outer 111";
    5. public void outFun2() {
    6. Inner1 obj1 = new Inner1();
    7. Inner1 obj2 = new Inner1();
    8. System.out.println(obj1 == obj2);//false
    9. System.out.println(obj1.getInnFlied1());//inner 333
    10. System.out.println(Inner1.getInnerStaticFiled2());//inner static 444
    11. }
    12. public String getOutFiled() {
    13. return this.outFiled;
    14. }
    15. //静态嵌套类
    16. // private static class Inner1
    17. // static class Inner1
    18. // protected static class Inner1
    19. public static class Inner1 {
    20. static String innerStaticFiled2 = "inner static 444";
    21. //静态嵌套类可以定义静态的、非静态的成员
    22. private String innFlied1 = "inner 333";
    23. public static String getInnerStaticFiled2() {
    24. hello();
    25. //Outer2.hello();
    26. return innerStaticFiled2;
    27. }
    28. private static void hello() {
    29. System.out.println("inner hello");
    30. }
    31. public String getInnFlied1() {
    32. return innFlied1;
    33. }
    34. public void innFun1() {
    35. //静态嵌套类可以直接访问包围类的静态成员
    36. System.out.println(outStaticFiled2);
    37. //System.out.println(outFiled);--error
    38. //静态嵌套类可以通过对象访问包围类的非静态成员
    39. Outer2 obj = new Outer2();
    40. System.out.println(obj.getOutFiled());
    41. }
    42. }
    43. }

    https://blog.csdn.net/weixin_45082647/article/details/106558172?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165079342116781667880264%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=165079342116781667880264&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-2-106558172.142^v9^control,157^v4^control&utm_term=java%E5%B5%8C%E5%A5%97%E7%B1%BB&spm=1018.2226.3001.4187