Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Anonymous Class in Java
- public static void main(String[] args) {
- AnonymousClass a = new AnonymousClass() {
- int whatever = 1;
- };
- System.out.println(a.whatever);
- }
- public static void main(String[] args) {
- System.out.println(new Object() {
- int whatever = 1;
- }.whatever);
- }
- public static void main(String[] args) {
- class Local {
- int whatever = 1;
- }
- Local local = new Local();
- System.out.println(local);
- }
- public static void main(String[] args) {
- class NotAnonymous {
- public int whatever = 1;
- }
- NotAnonymous na = new NotAnonymous();
- System.out.println(na.whatever);
- }
- private interface AnonymousClass {
- }
- public static void main(String[] args) {
- AnonymousClass a = new AnonymousClass() {
- int whatever = 1;
- };
- System.out.println(a.whatever); // this won't work
- }
Add Comment
Please, Sign In to add comment