" นอกจากวิธีแบบสแตติกแล้ว ยังมีคลาสแบบสแตติกอีกด้วยเราจะพูดถึงรายละเอียดเหล่านี้ในภายหลัง สำหรับตอนนี้ ผมขอแสดงตัวอย่างให้คุณเห็น:"

ตัวอย่าง:
public class StaticClassExample
{
    private static int catCount = 0;

    public static void main(String[] args) throws Exception
    {
        Cat bella = new Cat("Bella");
        Cat tiger = new Cat("Tiger");

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

     public static class Cat
    {
        private String name;

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

}
2
งาน
Java Syntax,  ระดับบทเรียน
ล็อค
Code entry
Your attention, please! Now recruiting code entry personnel for CodeGym. So turn up your focus, let your fingers relax, read the code, and then... type it into the appropriate box. Code entry is far from a useless exercise, though it might seem so at first glance: it allows a beginner to get used to and remember syntax (modern IDEs seldom make this possible).

" คุณสามารถสร้างอ็อบเจกต์ Cat ได้มากเท่าที่คุณต้องการแต่นี่ไม่ใช่กรณีของตัวแปรสแตติก มีเพียงสำเนาเดียวของตัวแปรสแตติกเท่านั้นที่มีอยู่"

"จุดประสงค์หลักของการใช้static modifier ในการประกาศคลาสคือเพื่อควบคุมความสัมพันธ์ระหว่าง คลาส CatและStaticClassExampleแนวคิดคร่าวๆ คือ: คลาส Cat ไม่ได้เชื่อมโยงกับออบเจกต์ StaticClassExampleและไม่สามารถเข้าถึงอินสแตนซ์ได้ (ไม่ใช่ คงที่) ตัวแปรของคลาส StaticClassExample"

"ฉันสามารถสร้างชั้นเรียนภายในชั้นเรียนได้หรือไม่"

"ใช่ Java อนุญาต แต่อย่าเพิ่งคิดมากเกินไป มันจะชัดเจนขึ้นเมื่อฉันอธิบายบางสิ่งเพิ่มเติมให้คุณในอนาคต"

“ฉันก็หวังอย่างนั้น ริชิ”