Created
April 13, 2019 08:35
-
-
Save LeaKoroliuk/c53618736e0dd5324a1395797e297022 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package lesson15.inner_classes; | |
| public class Outer { | |
| private int a = 4; | |
| private double d = 3.14; | |
| private static String str = "u"; // можно делать статические приватные поля и обращаться к ним с внутрених классов | |
| int method() { | |
| new Inner().hello(); | |
| System.out.println(); | |
| return 1; | |
| } | |
| static void m() { | |
| System.out.println("Hello from Outer.m()!"); | |
| new Inner().hello(); | |
| System.out.println(); | |
| } | |
| public static class Inner { | |
| void hello() { | |
| System.out.println("Hello from static nested class!"); | |
| System.out.println("str = " + str); | |
| str = "12345"; | |
| System.out.println("str = " + str); | |
| System.out.println(); | |
| } | |
| } | |
| // Если так, тогда в другом классе нельзя создать экземпляр этого класса (только в том, к котором он находится) | |
| // ОШИБКА 'Outer.Inner' has private access in 'Outer' | |
| // private static class Inner { | |
| // void hello() { | |
| // System.out.println("Hello from static nested class!"); | |
| // System.out.println("str = " + str); | |
| // str = "12345"; | |
| // System.out.println("str = " + str); | |
| // System.out.println(); | |
| // } | |
| // } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment