main, abs, read, and other static Methodsmain - execution consists of objects invoking methods of other objects,
but how does the process begin?
abs - operates on int which is a primitive type
read - no object yet
static Methods to the Rescue
Static methods
Static Data - State for Static Methods
Static variables
read Methods
Need to create the object before can invoke its methods
class Name {
Name(String last, String first) {
this.last = last;
this.first = first;
}
static Name read(BufferedReader br) {
String line = br.readLine();
if (line == null) return null; // No more names in file
line = line.trim();
int pos = line.indexOf(",");
String last = line.substring(0, pos);
line = line.substring(pos+1).trim();
pos = line.indexOf(",");
String first = line.substring(0, pos);
line = line.substring(pos+1).trim();
return new Name(last, first);
}