Before you look at these APIs in depth, you need to learn what the keywords final and static mean, as they frequently come up in the API documentation.
- When used with a primitive data type, final means that the value of the variable will never change. This is useful in many cases, such as tax rates, math constants such as PI, and base values that are used in several places in your code. Identifiers with final are generally made with only capital letters so they are easily distinguishable from the rest of the code.
final double TAXRATE = 0.0825;
final double ROUNDS_IN_GAME = 100;
A program that repeatedly uses a constant identifier, such as TAXRATE, can be quickly modified by assigning a value to TAXRATE at the top of the program. If the value of the tax rate is hard coded everywhere it is used, then changing the tax rate would involve changing that value everywhere it appears in the program. This involves searching through your program and finding every single time that value is used, and then changing it to the new value. It would be easy to accidentally miss a value or two or possibly change something that was similar but wasn’t supposed to be the tax rate. Using final values will not only save time but can also reduce the number of errors in your program.
Once a final variable is given a value within a program, that value may never change in that run. In order to change that value, you must change it within your code, recompile, and run the program again. You may still define this value within the constructors so that the value can be determined at run time; it need not be defined at the same time that the variable is declared.
-
Objects and methods can also take a final keyword. However, they behave differently than primitive data types. We have not yet discussed the concepts that these final objects and methods affect, but we will cover them in Lesson A11 - Inheritance.
-
Using the keyword static means that the data member or method is attached to the class rather than to an object of that class.
- With a static method, you only need to type the name of the class followed by the name of the method. You never need to create a new object when dealing with static methods. You can think of static methods as belonging to the class itself, whereas non-static methods are attached to the objects created from that class.
int jason = Math.pow(3,4);
jason
receives and stores the result of 3 to the 4th power, which is 81.
Notice how there was never any need to create an object of type Math. Instead, we just have an int assigned the value created by the static pow method of the Math class.
- Data members that are static may also be used without creating an object of that class. They also have specific behavior when using that value of the data member within the class. While we may make a hundred objects from one class, any static variables of that class will in fact be shared by each of those objects. If one object changes the value of that static variable, then the value is changed for all of the other objects of that type. This is because the variable and its value do not belong to any of the objects individually, but to the class itself.