Skip to content

Instantly share code, notes, and snippets.

@vivek-vijayan
Created August 8, 2022 08:46
Show Gist options
  • Save vivek-vijayan/4244f1ae3cf025a850e7f389516e9407 to your computer and use it in GitHub Desktop.
Save vivek-vijayan/4244f1ae3cf025a850e7f389516e9407 to your computer and use it in GitHub Desktop.
class Parent {
private int a = 10;
void addTwo() {
a += 2;
}
void showValueOfA() {
System.out.println(a);
}
}
public class Main {
public static void main(String[] args) {
// anonymous class
new Parent() {{
// no need to mention the keyword "this"
addTwo();
addTwo();
addTwo();
showValueOfA();
}};
}
}
// Output: 16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment