1.统计产生对象的数量。
class Book{
private String title ;
private static int num = 0 ;
public Book (String book){
num ++ ;
System.out.println("This is the num" + num + "book" ) ;
}
}
public class Static{
public static void main(String args[]){
Book b1 = new Book ("..") ;
Book b2 = new Book ("..") ;
Book b3 = new Book ("..") ;
Book b4 = new Book ("..") ;
Book b5 = new Book ("..") ;
}
}
2.自动获得编号
class Book{
private String title ;
private static int num = 0 ;
public Book(){
this("NOTITL" + num++ ) ;
}
public Book(String title){
this.title = title ;
}
public String getTitle(){
return this.title ;
}
}
public class Static{
public static void main(String args[]){
System.out.println(new Book().getTitle()) ;
System.out.println(new Book().getTitle()) ;
System.out.println(new Book().getTitle()) ;
System.out.println(new Book().getTitle()) ;
System.out.println(new Book().getTitle()) ;
}
}