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. // a short sample Book class, nothing fancy
  8. class Book {
  9. public static String staticTitle; // one variable static
  10. public String dynamicTitle; // one variable not static
  11.  
  12. public Book(String title) {
  13. // both variables are set to the same value
  14. dynamicTitle = title;
  15. staticTitle = title;
  16. }
  17.  
  18. // to make it easier, a toString method
  19. public String toString() {
  20. return "Static title: " + staticTitle + "\nDynamic title: " + dynamicTitle + "\n";
  21. }
  22. }
  23.  
  24.  
  25. /* Name of the class has to be "Main" only if the class is public. */
  26. class Ideone
  27. {
  28. public static void main (String[] args) throws java.lang.Exception
  29. {
  30. // now, we are going to create and print a couple Book objects:
  31. Book book1 = new Book("Call of the Wild");
  32. // let's print book1
  33. System.out.println("Book 1 has just been created.");
  34. System.out.println("Book 1 contains: " + book1); // this implicitly calls "toString"
  35.  
  36. // another book
  37. System.out.println("Book 2 has just been created.");
  38. Book book2 = new Book("Moby Dick");
  39. // and print it again
  40. System.out.println("Book 2 contains: " + book2);
  41.  
  42. // last, let's print book1 again:
  43. System.out.println("Content after both books have been created");
  44. System.out.println("Book 1 contains: " + book1);
  45. System.out.println("Book 2 contains: " + book2);
  46.  
  47.  
  48. }
  49. }
Success #stdin #stdout 0.1s 27824KB
stdin
Standard input is empty
stdout
Book 1 has just been created.
Book 1 contains: Static title: Call of the Wild
Dynamic title: Call of the Wild

Book 2 has just been created.
Book 2 contains: Static title: Moby Dick
Dynamic title: Moby Dick

Content after both books have been created
Book 1 contains: Static title: Moby Dick
Dynamic title: Call of the Wild

Book 2 contains: Static title: Moby Dick
Dynamic title: Moby Dick