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

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
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