<< 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.

Back to Question 7

Answer 6

D. 3.1415927

Explanation 6

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

Back to Question 6

Answer 5

C. 5

Explanation 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.

Back to Question 5

Answer 4

A. Compiler Error

Explanation 4

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:

Questions & Answers

how to study physic and understand
Ewa Reply
what is conservative force with examples
Moses
what is work
Fredrick Reply
the transfer of energy by a force that causes an object to be displaced; the product of the component of the force in the direction of the displacement and the magnitude of the displacement
AI-Robot
why is it from light to gravity
Esther Reply
difference between model and theory
Esther
Is the ship moving at a constant velocity?
Kamogelo Reply
The full note of modern physics
aluet Reply
introduction to applications of nuclear physics
aluet Reply
the explanation is not in full details
Moses Reply
I need more explanation or all about kinematics
Moses
yes
zephaniah
I need more explanation or all about nuclear physics
aluet
Show that the equal masses particles emarge from collision at right angle by making explicit used of fact that momentum is a vector quantity
Muhammad Reply
yh
Isaac
A wave is described by the function D(x,t)=(1.6cm) sin[(1.2cm^-1(x+6.8cm/st] what are:a.Amplitude b. wavelength c. wave number d. frequency e. period f. velocity of speed.
Majok Reply
what is frontier of physics
Somto Reply
A body is projected upward at an angle 45° 18minutes with the horizontal with an initial speed of 40km per second. In hoe many seconds will the body reach the ground then how far from the point of projection will it strike. At what angle will the horizontal will strike
Gufraan Reply
Suppose hydrogen and oxygen are diffusing through air. A small amount of each is released simultaneously. How much time passes before the hydrogen is 1.00 s ahead of the oxygen? Such differences in arrival times are used as an analytical tool in gas chromatography.
Ezekiel Reply
please explain
Samuel
what's the definition of physics
Mobolaji Reply
what is physics
Nangun Reply
the science concerned with describing the interactions of energy, matter, space, and time; it is especially interested in what fundamental mechanisms underlie every phenomenon
AI-Robot
what is isotopes
Nangun Reply
nuclei having the same Z and different N s
AI-Robot
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask