fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static Object[] objects = new Object[2];
  11.  
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. M1();
  15. M2();
  16. System.gc();
  17. }
  18.  
  19. static void M1() {
  20. objects[0] = new Foo().Bar();
  21. }
  22. static void M2() {
  23. objects[1] = new Foo().Baz();
  24. }
  25. }
  26.  
  27. class Foo {
  28. static int i = 0;
  29. int j = i++;
  30.  
  31. public Foo() {
  32. System.out.println("Constructed: " + j);
  33. }
  34.  
  35. Object Bar() {
  36. return new Object() {
  37.  
  38. };
  39. }
  40. static Object Baz() {
  41. return new Object() {
  42.  
  43. };
  44. }
  45.  
  46. protected void finalize() throws Throwable {
  47. System.out.println("Garbage collected " + j);
  48. }
  49. }
Success #stdin #stdout 0.11s 320512KB
stdin
Standard input is empty
stdout
Constructed: 0
Constructed: 1
Garbage collected 1