Skip to content

Instantly share code, notes, and snippets.

@vinimonteiro
Created July 11, 2022 06:41
Show Gist options
  • Save vinimonteiro/1bf24c7d2a0d360579f4330ebbc2e237 to your computer and use it in GitHub Desktop.
Save vinimonteiro/1bf24c7d2a0d360579f4330ebbc2e237 to your computer and use it in GitHub Desktop.
NestedClasses
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