//: c04:Garbage.java // From 'Thinking in Java, 2nd ed.' by Bruce Eckel // www.BruceEckel.com. See copyright notice in CopyRight.txt. // Demonstration of the garbage // collector and finalization // // import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.util.*; import com.bruceeckel.swing.*; import com.bruceeckel.util.*; public class Garbage extends JApplet { JTextArea Xout = new JTextArea(10, 50); boolean f = false; int created = 0; public void init() { Container cp = getContentPane(); cp.setLayout(new FlowLayout()); cp.add(new JLabel("Garbage!")); cp.add(new JScrollPane(Xout)); // As long as the flag hasn't been set, make Chairs: while(!f) { new Chair(); } Xout.append( "\nTotal Chairs created = " + created ); } class Chair { int i; Chair() { i = ++created; } public void finalize() { f = true; } } public static void main(String[] args) { Console.run(new Garbage(), 600, 250); } } ///:~