" Naast statische methoden zijn er statische klassen. We zullen deze later in meer detail bespreken. Laat me je nu een voorbeeld laten zien:"

Voorbeeld:
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
Taak
Java Syntax,  niveau 6les 7
Vergrendeld
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).

" U kunt zoveel Cat-objecten maken als u wilt. Maar dit is niet het geval met een statische variabele. Er bestaat slechts één kopie van een statische variabele."

"Het belangrijkste doel van het gebruik van de statische modifier in de klassendeclaratie is om de relatie tussen de Cat- en StaticClassExample- klassen te regelen. Het idee is grofweg dit: de Cat-klasse is niet gekoppeld aan StaticClassExample-objecten en heeft geen toegang tot de instantie (niet- static) variabelen van de klasse StaticClassExample."

"Dus ik kan klassen binnen klassen maken?"

"Ja. Java staat dat toe, maar denk er nu niet te veel over na. Het zal duidelijker worden als ik je in de toekomst nog wat meer uitleg."

"Ik hoop het, Rishi."