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. import java.util.concurrent.atomic.AtomicInteger;
  8.  
  9. abstract class Ticket {
  10. private static final AtomicInteger ticketCounter = new AtomicInteger();
  11. private final int ticketId;
  12.  
  13. public Ticket() {
  14. this.ticketId = ticketCounter.incrementAndGet();
  15. }
  16.  
  17. public int getTicketId() {
  18. return ticketId;
  19. }
  20. }
  21.  
  22. class Ticket1 extends Ticket {}
  23. class Ticket2 extends Ticket {}
  24. class Ticket3 extends Ticket {}
  25.  
  26. /* Name of the class has to be "Main" only if the class is public. */
  27. class Ideone
  28. {
  29. public static void main (String[] args) throws java.lang.Exception
  30. {
  31. Ticket1 t1 = new Ticket1();
  32. Ticket3 t3 = new Ticket3();
  33. Ticket2 t2 = new Ticket2();
  34. Ticket1 t4 = new Ticket1();
  35. System.out.println("t1:"+t1.getTicketId());
  36. System.out.println("t2:"+t2.getTicketId());
  37. System.out.println("t3:"+t3.getTicketId());
  38. System.out.println("t4:"+t4.getTicketId());
  39. }
  40. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
t1:1
t2:3
t3:2
t4:4