Java umožňuje definovať triedu v rámci inej triedy. Tieto sú tzv Vnorené triedy . Triedy môžu byť statické, čo si väčšina vývojárov uvedomuje, odteraz môžu byť niektoré triedy v Jave statické. Java podporuje Premenné statickej inštancie , Statické metódy , Statický blok a statické triedy. Trieda, v ktorej je definovaná vnorená trieda, je známa ako Vonkajšia trieda . Na rozdiel od tried najvyššej úrovne, Vnorené triedy môžu byť statické . Nestatické vnorené triedy sú známe aj ako Vnútorné triedy .
Poznámka: Trieda najvyššej úrovne nemôže byť v jazyku Java statická, na vytvorenie statickej triedy musíme vytvoriť vnorenú triedu a potom ju urobiť statickou.
Inštanciu vnútornej triedy nemožno vytvoriť bez inštancie vonkajšej triedy. Preto môže inštancia vnútornej triedy pristupovať ku všetkým členom svojej vonkajšej triedy bez použitia odkazu na inštanciu vonkajšej triedy. Z tohto dôvodu môžu vnútorné triedy pomôcť urobiť programy jednoduchými a stručnými.
Pamätajte: V statickej triede môžeme jednoducho vytvárať objekty.
Rozdiely medzi statickými a nestatickými vnorenými triedami
Nasledujú hlavné rozdiely medzi statickými vnorenými triedami a vnútornými triedami.
- Statická vnorená trieda môže byť vytvorená bez vytvorenia inštancie jej vonkajšej triedy.
- Vnútorné triedy môžu pristupovať k statickým aj nestatickým členom vonkajšej triedy. Statická trieda má prístup iba k statickým členom vonkajšej triedy.
Príklad z Statické a nestatické vnorené triedy
Nižšie je uvedená implementácia témy uvedenej vyššie:
Java
// 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();> > }> }> |
>
>Výkon
Message from nested static class: GeeksForGeeks Message from non-static nested class: GeeksForGeeks Message from non-static nested class: GeeksForGeeks>