Class 1, Part 2 |
Intro to Java 30-IT-396 |
|
Static Fields, Constants
-
We know that fields declared static have only one occurrence per
class.
-
You do not need an instance of a class to access a static field or method.
So, you can access the field by the class name itself, as in Car.wheels
= 4;
-
Or, you can access the field by the object, such as myCar.wheels = 4;
-
Either way, it changes the value of wheels for all instances of
class Car.
-
A static constant is a field that is both static and final. That
is, there is only one field no matter how many objects, and the value of
that field cannot be changed once the program is running.
-
public static final String institution = "University of Cincinnati";
-
Since this is a constant and cannot be changed, it is just fine to declare
it public. But keep in mind, if a field can be changed, we will make
it private.
Static Methods, Factory Methods


Created by: Brandan
Jones January 4, 2002