A Better Potato
class Potato {
public Potato(String name) {
this.name = name ; // Idiomatic use of this
num++ ;
}
public static int getNum() { // A static method
return num ;
}
private String name ; // Note: now private
private static int num = 0 ; // Also private
}
Potato p = new Potato(“one potato”), q = new Potato(“two potato”) ;
System.out.println(“There are ” + Potato.getNum() + “ potatoes”) ;