除了 static 方法,还有 static 类。我们稍后会更详细地讨论这些内容。现在,我们来看一个示例:”

示例:
public class StaticClassExample
{
    private static int catCount = 0;

    public static void main(String[] args) throws Exception
    {
        Cat bella = new Cat("贝拉");
        Cat tiger = new Cat("泰格");

        System.out.println("Cat 计数 " + catCount);
    }

     public static class Cat
    {
        private String name;

        public Cat(String name)
         {
            this.name = name;
            StaticClassExample.catCount++;
         }
     }

}

你可以根据需要创建任意数量的 Cat 对象。但这不适用于 static 变量。static 变量只能存在一个副本。”

“在类声明中使用 static 修饰符的主要目的是控制 CatStaticClassExample 类之间的关系。想法大致如下:Cat 类不链接到 StaticClassExample 对象,并且无法访问 StaticClassExample 类的实例(非 static)变量。”

“因此,我可以在类的内部创建类?”

“是的。Java 允许这样做,但现在不要过多考虑这个。等我将来进一步讲解时,你会了解得更清楚。”

“但愿如此,里希。”