Created
August 8, 2022 08:46
-
-
Save vivek-vijayan/4244f1ae3cf025a850e7f389516e9407 to your computer and use it in GitHub Desktop.
This file contains 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
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