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.
final
keywordWhen 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;
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.
static
keywordUsing 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.
Last modified: December 12, 2022
Back to Understanding Apis