Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package anobject;
- import java.util.Objects;
- final class Example extends Object {
- static {
- count = 0;
- }
- private static Integer count;
- private final Integer id;
- public Example() {
- this.id = ++count;
- //
- System.out.println("This is the creation of an " + this.toString() + '.');
- System.out.flush();
- }
- public static Integer getCount() {
- return count;
- }
- public Integer getId() {
- return id;
- }
- public void display() {
- System.out.println("This is the display of an " + this.toString() + '.');
- System.out.flush();
- }
- @Override
- public String toString() {
- return "Example{id=" + id + ", count=" + count + '}';
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- final Example other = (Example) obj;
- return Objects.equals(this.id, other.id);
- }
- @Override
- protected void finalize() throws Throwable {
- System.out.println("This is the destruction of an " + this.toString() + '.');
- System.out.flush();
- //
- count--;
- super.finalize();
- }
- @Override
- protected Object clone() throws CloneNotSupportedException {
- throw new CloneNotSupportedException();
- }
- }
- /**
- *
- * @author Aidan
- */
- public class AnObject {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- { // Put instances in this block.
- Example a = new Example(),
- b = new Example(),
- c = new Example(),
- d = new Example();
- //
- a.display();
- b.display();
- c.display();
- d.display();
- //
- try {
- a.finalize();
- b.finalize();
- c.finalize();
- d.finalize();
- } catch (Throwable ex) {
- System.err.println("Destructive error.");
- System.err.flush();
- }
- }
- System.out.println("Done.");
- System.out.flush();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement