Advertisement
HasteBin0

Java Object DEMO

Jun 30th, 2017
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. package anobject;
  2.  
  3. import java.util.Objects;
  4.  
  5. final class Example extends Object {
  6.  
  7.     static {
  8.         count = 0;
  9.     }
  10.  
  11.     private static Integer count;
  12.     private final Integer id;
  13.  
  14.     public Example() {
  15.         this.id = ++count;
  16.         //
  17.         System.out.println("This is the creation of an " + this.toString() + '.');
  18.         System.out.flush();
  19.     }
  20.  
  21.     public static Integer getCount() {
  22.         return count;
  23.     }
  24.  
  25.     public Integer getId() {
  26.         return id;
  27.     }
  28.  
  29.     public void display() {
  30.         System.out.println("This is the display of an " + this.toString() + '.');
  31.         System.out.flush();
  32.     }
  33.  
  34.     @Override
  35.     public String toString() {
  36.         return "Example{id=" + id + ", count=" + count + '}';
  37.     }
  38.  
  39.     @Override
  40.     public boolean equals(Object obj) {
  41.         if (this == obj) {
  42.             return true;
  43.         }
  44.         if (obj == null) {
  45.             return false;
  46.         }
  47.         if (getClass() != obj.getClass()) {
  48.             return false;
  49.         }
  50.         final Example other = (Example) obj;
  51.         return Objects.equals(this.id, other.id);
  52.     }
  53.  
  54.     @Override
  55.     protected void finalize() throws Throwable {
  56.         System.out.println("This is the destruction of an " + this.toString() + '.');
  57.         System.out.flush();
  58.         //
  59.         count--;
  60.         super.finalize();
  61.     }
  62.  
  63.     @Override
  64.     protected Object clone() throws CloneNotSupportedException {
  65.         throw new CloneNotSupportedException();
  66.     }
  67. }
  68.  
  69. /**
  70.  *
  71.  * @author Aidan
  72.  */
  73. public class AnObject {
  74.  
  75.     /**
  76.      * @param args the command line arguments
  77.      */
  78.     public static void main(String[] args) {
  79.         { // Put instances in this block.
  80.             Example a = new Example(),
  81.                     b = new Example(),
  82.                     c = new Example(),
  83.                     d = new Example();
  84.             //
  85.             a.display();
  86.             b.display();
  87.             c.display();
  88.             d.display();
  89.             //
  90.             try {
  91.                 a.finalize();
  92.                 b.finalize();
  93.                 c.finalize();
  94.                 d.finalize();
  95.             } catch (Throwable ex) {
  96.                 System.err.println("Destructive error.");
  97.                 System.err.flush();
  98.             }
  99.         }
  100.         System.out.println("Done.");
  101.         System.out.flush();
  102.     }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement