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. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. System.out.println("Instanciando ClasseA...");
  13. ClasseA a = new ClasseA();
  14. System.out.println("No. de instancias: " + ClasseA.getNumberInstances());
  15.  
  16. System.out.println("Instanciando ClasseB...");
  17. ClasseB b = new ClasseB();
  18. System.out.println("No. de instancias: " + ClasseA.getNumberInstances());
  19.  
  20. System.out.println("Instanciando ClasseC...");
  21. ClasseC c = new ClasseC();
  22. System.out.println("No. de instancias: " + ClasseA.getNumberInstances());
  23. }
  24.  
  25. }
  26.  
  27. class ClasseA {
  28.  
  29. private static int instances = 0;
  30.  
  31. public ClasseA() {
  32. instances++;
  33. }
  34.  
  35. public static int getNumberInstances(){
  36. return instances;
  37. }
  38.  
  39. }
  40.  
  41.  
  42. class ClasseB extends ClasseA {
  43.  
  44. }
  45.  
  46. class ClasseC extends ClasseA{
  47.  
  48. }
Success #stdin #stdout 0.06s 27844KB
stdin
Standard input is empty
stdout
Instanciando ClasseA...
No. de instancias: 1
Instanciando ClasseB...
No. de instancias: 2
Instanciando ClasseC...
No. de instancias: 3