A (175)

(Tuis.) #1

154 CHAPTER 5: Introduction to Java: Objects, Methods, Classes, and Interfaces


To use a variable from the Car class example, if you wanted all Car objects to reference the fuel
variable, which is set in the code to “Gas” at the class level, and wanted that fuel variable to belong
only to the class, and not to any of the Car objects that will be created using that class, you would
declare the fuel variable as follows:


static String fuel = "Gas";


The static modifier keyword works in much the same way for methods that are declared as static,
thus a static modifier would be utilized to create methods that are intended to exist independently of
any object instances created using the class. This again “fixes” the method in place, so it is the only
“copy” of that method that will be used by your class and objects from that class.


A static method can be referenced using the class name and dot notation even without an object
instance of the class ever being created. For instance, if you declared the .applyBrake( ) method to
be static, you could reference it using the code statement Car.applyBrake( ) even without having
created a Car object using the new keyword.


Static methods cannot use any instance variables of any object instances created using the class
in which they are defined, until one of those object instances has been created. Static methods
should take all their data values from the incoming parameter list, and then compute something from
those parameters, with no reference to variables, which are inherently not “static” because they’re
variable!


It is possible to access your class variables and methods inside of a static method. Use the class
name followed by a period and then the name of the variable or method, so using your Car class
example, you could access your Car object’s speed variable using Car.speed, or its .shiftGears( )
method, by using for instance a Car.shiftGears(4) method call, similar to what you would do off of a
Car object instance.


So to recode your .applyBrake(int brakingFactor) method as static and reference the class speed
variable, you would modify your method to look something like this:


public static void applyBrake (int brakingFactor) {
Car.speed = Car.speed - brakingFactor;
}


Notice that the access control modifier comes first, then the non-access modifier, and then finally
the return data type declaration comes last in the list. This is the modifier and declaration ordering
convention for the Java programming language. Next, let’s look at the final modifier, which
sometimes gets confused with the static modifier, as the final modifier also means that something
cannot be changed, and is thus “fixed” as well! Java can be confusing in a number of areas, and this
happens to be one of them!


The final Keyword


You can define a class using the final modifier keyword, and if a class is final, it cannot be subclassed.
This is usually implemented for reasons of Java security, so tested, mission-critical Java code
cannot be modified or changed. You will notice as we get deeper into the Android API and Java that
many standard Java library classes are declared using the final modifier keyword. As an example,

Free download pdf