logo

Klasa statyczna w Javie

Java umożliwia zdefiniowanie klasy w obrębie innej klasy. Są to tzw Klasy zagnieżdżone . Klasy mogą być statyczne, o czym wie większość programistów, dlatego też niektóre klasy mogą być statyczne w Javie. Obsługuje Javę Statyczne zmienne instancji , Metody statyczne , Blok statyczny i Klasy statyczne. Klasa, w której zdefiniowano klasę zagnieżdżoną, nazywana jest klasą Klasa zewnętrzna . W odróżnieniu od zajęć na najwyższym poziomie, Klasy zagnieżdżone mogą być statyczne . Niestatyczne klasy zagnieżdżone są również znane jako Klasy wewnętrzne .

Notatka: Klasa najwyższego poziomu nie może być statyczna w Javie. Aby utworzyć klasę statyczną, musimy utworzyć klasę zagnieżdżoną, a następnie uczynić ją statyczną.



Nie można utworzyć instancji klasy wewnętrznej bez instancji klasy zewnętrznej. Dlatego instancja klasy wewnętrznej może uzyskać dostęp do wszystkich elementów swojej klasy zewnętrznej bez użycia odniesienia do instancji klasy zewnętrznej. Z tego powodu klasy wewnętrzne mogą pomóc w tworzeniu prostych i zwięzłych programów.

Pamiętać: W klasie statycznej możemy łatwo tworzyć obiekty.

Różnice między statycznymi i niestatycznymi klasami zagnieżdżonymi

Poniżej przedstawiono główne różnice między statycznymi klasami zagnieżdżonymi a klasami wewnętrznymi.



  1. Instancję statycznej klasy zagnieżdżonej można utworzyć bez tworzenia instancji jej klasy zewnętrznej.
  2. Klasy wewnętrzne mogą uzyskiwać dostęp zarówno do statycznych, jak i niestatycznych elementów klasy zewnętrznej. Klasa statyczna może uzyskać dostęp tylko do statycznych elementów klasy zewnętrznej.

Przykład z Klasy zagnieżdżone statyczne i niestatyczne

Poniżej realizacja powyższego tematu:

Jawa






// Java program to Demonstrate How to> // Implement Static and Non-static Classes> // Class 1> // Helper class> class> OuterClass {> >// Input string> >private> static> String msg =>'GeeksForGeeks'>;> >// Static nested class> >public> static> class> NestedStaticClass {> >// Only static members of Outer class> >// is directly accessible in nested> >// static class> >public> void> printMessage()> >{> >// Try making 'message' a non-static> >// variable, there will be compiler error> >System.out.println(> >'Message from nested static class: '> + msg);> >}> >}> >// Non-static nested class -> >// also called Inner class> >public> class> InnerClass {> >// Both static and non-static members> >// of Outer class are accessible in> >// this Inner class> >public> void> display()> >{> >// Print statement whenever this method is> >// called> >System.out.println(> >'Message from non-static nested class: '> >+ msg);> >}> >}> }> // Class 2> // Main class> class> GFG {> >// Main driver method> >public> static> void> main(String args[])> >{> >// Creating instance of nested Static class> >// inside main() method> >OuterClass.NestedStaticClass printer> >=>new> OuterClass.NestedStaticClass();> >// Calling non-static method of nested> >// static class> >printer.printMessage();> >// Note: In order to create instance of Inner class> >// we need an Outer class instance> >// Creating Outer class instance for creating> >// non-static nested class> >OuterClass outer =>new> OuterClass();> >OuterClass.InnerClass inner> >= outer.>new> InnerClass();> >// Calling non-static method of Inner class> >inner.display();> >// We can also combine above steps in one> >// step to create instance of Inner class> >OuterClass.InnerClass innerObject> >=>new> OuterClass().>new> InnerClass();> >// Similarly calling inner class defined method> >innerObject.display();> >}> }>

>

>

Wyjście

Message from nested static class: GeeksForGeeks Message from non-static nested class: GeeksForGeeks Message from non-static nested class: GeeksForGeeks>