Advertisement
Guest User

another example of static

a guest
Mar 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. Book.java
  2. -----------------------------------------------------------------
  3. public class Book {
  4.     private static int idCount = 0;
  5.     private int id;
  6.     private String name;
  7.     private String author;
  8.     private int pageCount;
  9.  
  10.     public Book(String name, String author, int pageCount){
  11.         this.name = name;
  12.         this.author = author;
  13.         this.pageCount = pageCount;
  14.         this.id = idCount;
  15.         idCount += 1;
  16.     }
  17.  
  18.     @Override
  19.     public String toString() {
  20.         return String.format("id: %d, name: %s, author: %s, number of pages: %d",id ,name, author, pageCount);
  21.     }
  22.  
  23.     public static int getBookCount(){
  24.         return idCount;
  25.     }
  26. }
  27.  
  28.  
  29. -------------------------------------------------------------
  30.  
  31. Tester.java
  32. -------------------------------------------------------------
  33.  
  34.  
  35. public class Tester {
  36.     public static void main(String[] args){
  37.         Book b1 = new Book("Book1", "Author1", 10000);
  38.         Book b2 = new Book("Book2", "Author2", 25);
  39.         Book b3 = new Book("Book3", "Author2", 25);
  40.         Book b4 = new Book("Book4", "Author2", 25);
  41.         Book b5 = new Book("Book5", "Author2", 25);
  42.  
  43.         System.out.println(b1);
  44.         System.out.println(b5);
  45.         System.out.println(b3);
  46.         System.out.println(b4);
  47.         System.out.println(b2);
  48.  
  49.         System.out.println("the total number of book is: "+Book.getBookCount());
  50.  
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement