[sourcecode=’java’]
public class Student {
private static int totalStudents;
// Other details omitted.
// Constructor.
public Student() {
// Details omitted.
// Automatically increment the student count every time we
// instantiate a new Student.
totalStudents++;
}
// The following three methods are now static methods; .
public static int getTotalStudents() {
return totalStudents;
}
public static void setTotalStudents(int x) {
totalStudents = x;
}
public static void reportTotalEnrollment() {
System.out.println(“Total Enrollment: ” + getTotalStudents());
}
public static void main (String[] args) {
//Static methods can be invoked from a class filename
//or invoke it from the object related class
setTotalStudents(4);
reportTotalEnrollment();
//optional
/*
*Student s1 = new Student();
*s1.setTotalStudent();
*s1.reportTotalEnrollment();
*/
}
}
[/sourcecode]