Guest User

Untitled

a guest
Aug 10th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. Anonymous Class in Java
  2. public static void main(String[] args) {
  3.  
  4. AnonymousClass a = new AnonymousClass() {
  5. int whatever = 1;
  6. };
  7.  
  8. System.out.println(a.whatever);
  9. }
  10.  
  11. public static void main(String[] args) {
  12. System.out.println(new Object() {
  13. int whatever = 1;
  14. }.whatever);
  15. }
  16.  
  17. public static void main(String[] args) {
  18. class Local {
  19. int whatever = 1;
  20. }
  21. Local local = new Local();
  22. System.out.println(local);
  23. }
  24.  
  25. public static void main(String[] args) {
  26. class NotAnonymous {
  27. public int whatever = 1;
  28. }
  29. NotAnonymous na = new NotAnonymous();
  30. System.out.println(na.whatever);
  31. }
  32.  
  33. private interface AnonymousClass {
  34.  
  35. }
  36.  
  37. public static void main(String[] args) {
  38.  
  39. AnonymousClass a = new AnonymousClass() {
  40. int whatever = 1;
  41. };
  42.  
  43. System.out.println(a.whatever); // this won't work
  44. }
Add Comment
Please, Sign In to add comment