<< Chapter < Page | Chapter >> Page > |
(A method that is declared static is commonly referred to as a class method. If the method is not declared public , it may not be accessible from your code.)
Accessing the static method
This is illustrated by the following fragment from the program, with much of the code deleted for brevity.
//...
Worker.staticMethod();//...
class Worker{public static void staticMethod(){
//...}//end staticMethod()}// end class
The class named Worker defines a public static method named staticMethod . A statement in the main method of the controlling class calls the method by referring to the name of theclass and the name of the method joined by a period.
When should you use static methods?
Static methods are very useful as utility methods (getting the absolute value of a number, for example) .
In my opinion, you should almost never use a static method in any circumstance that requires the storage and use of data from one call ofthe method to the next. In other words, a static method may be appropriate for use when it performs a specific task that is completed each timeit is called without the requirement for data to persist between calls.
The Math class contains many good examples of the use of static methods, such as abs , acos , asin , etc.
D. 3.1415927
Using a public static final member variable
The class named Worker declares and initializes a member variable named fPi .
final
Because it is declared final , it is not possible to write code that will change its value after it has been initialized.
static
Because it is declared static , it can be accessed without a requirement to instantiate an object of the Worker class. All that is necessary to access the variable is to refer to the name of the classand the name of the variable joined by a period.
Because it is static , it can also be accessed by static methods.
public
Because it is declared public , it can be accessed by any code in any method in any object that can locate the class.
Type float is less precise than type double
Because the initialized value is cast from the type double that is returned by Math.PI to type float , an 8-digit approximation is stored in the variable named fPi .
The double value returned by Math.PI is 3.141592653589793
The cast to type float reduces the precision down to 3.1415927
C. 5
Using a final local variable
Well, I finally got rid of all the bugs. This program uses a final local variable properly. The program compiles and executes without any problems.
A. Compiler Error
The purpose of this question is to see if you are still awake.
What caused the compiler error?
The statement that caused the compiler error in this program is shown below. Now that you know that there was a compiler error, and you know which statementcaused it, do you know what caused it?
public final int x = 5;
Using public static final member variables
As I mentioned in an earlier question, the final keyword can be applied either to local variables or to member variables. When applying the final keyword to member variables, it is common practice to declare them to be both public and static in order to make them as accessible as possible. For example, the math class has a final variable that is described as follows:
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?