首页 > 解决方案 > 嵌套静态类和非静态类的区别

问题描述

我正在尝试可视化实时或假设示例(人体、公共汽车等自然界中的任何存在)以区分静态和非静态嵌套类。它有点奇怪的问题,但我认为它与现实世界有很好的关系:)

通常我遵循以下概念

class A
{
    class B
    {
        // static int x; not allowed here
    }

    static class C
    {
        static int x; // allowed here
    }
}

class Test
{
    public static void main(String… str)
    {
        A a = new A();

        // Non-Static Inner Class
        // Requires enclosing instance
        A.B obj1 = a.new B(); 

        // Static Inner Class
        // No need for reference of object to the outer class
        A.C obj2 = new A.C(); 
    }
}

但它只是代码和公式的代码。但是我很想知道人们如何理解任何假设的场景(如果有的话)。

请分享..!

标签: java

解决方案


推荐阅读