Created
July 11, 2022 06:41
-
-
Save vinimonteiro/1bf24c7d2a0d360579f4330ebbc2e237 to your computer and use it in GitHub Desktop.
NestedClasses
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 com.vinimo; | |
public class OuterClassExample1 { | |
private String name = "John"; | |
static int x = 1; | |
class InnerClass { | |
void access() { | |
System.out.println("name = " + name); | |
System.out.println("x = " + x); | |
} | |
} | |
static class StaticClass { | |
void access(OuterClassExample1 outer) { | |
//System.out.println("name = " + name); can't access the non-static field. | |
System.out.println("name = " + outer.name); | |
System.out.println("x = " + x); | |
} | |
} | |
public static void main(String[] args) { | |
OuterClassExample1 outerObject = new OuterClassExample1(); | |
OuterClassExample1.InnerClass innerObject = outerObject.new InnerClass(); | |
innerObject.access(); | |
StaticClass staticNestedObject = new StaticClass(); | |
staticNestedObject.access(outerObject); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment