Static Features
All objects belonging to a particular class to share a single value of a particular variable instead of having each object maintain its own copy of that variable as an attribute.
Static Variables
public class Student {
private static int totalStudents; // All the Student's objects will have the same value for this
private String name;
private String ssn;
public static int getTotalStudents() {
return totalStudents;
}
public static void setTotalStudents (int total) {
totalStudents = total;
}
public static void incrementEnrrollment() {
setTotalStudents(getTotalStudents() + 1);
}
}
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
Static Methods
Static methods are in turn methods that may be invoked on a class as a whole. Such methods are not permitted to access nonstatic features of the class to which the methods belong.
Student.incrementEnrrollment();
Student s1 = new Student();
s1.incrementEnrrollment();
An static method ay not be declared to be abstract.
public class Student {
public abstract static void incrementEnrrolment();
}
Revisiting the syntax of Print Statement
- System
class
- out
public
static
attribute of typePrintStream
- println
overloaded
public
method ofclass
PrintStream
System.out.println();
Utility Classes
We can take advantage of static features to design utility classes, which are classes that provide convenient ways of performing frequently used behaviors without having to instantiate an object to perform such behaviors. Such classes are often comprised wholly of static methods and public static variables.
Final KeyWord
The Java final
keyword can be applied to variables, methods, and classes as a whole. A final variable is a variable that can be assigned a value only once in a program; after that first assignment, the variable’s value cannot be changed. We declare such a variable by placing the final
keyword just before the type of the variable, as follows:
public class Student {
private final int x;
public static final int y;
public void some method() {
//final variable declaration and assignation in different statements
final z;
z = 3;
//This two lines will produce error
x = 1;
y = 2;
}
}
Public static final variables and interfaces
As stated earlier in the chapter, interfaces are not permitted to declare variables, but for one exception: as it turns out, interfaces are allowed to declare public static final variables to serve as global constants—that is, constant values that are in scope and hence accessible throughout an entire application.
public interface Administrator {
// Defining symbolic values to use as argument values for the second parameter of
// the hireProfessor method.
public static final int FULL_TIME = 1;
public static final int PART_TIME = 2;
// Valid values for workStatus are FULL_TIME (1) or PART_TIME (2).
public boolean hireProfessor(Professor p, int workStatus);
}
Administrator pAdmin = new Professor();
Professor p = new Professor();
// Hire p as a full-time faculty member.
pAdmin.hireProfessor(p, Administrator.FULL_TIME);
- A method declared to be final cannot be overridden in a subclass
- A class declared to be final cannot be subclassed