<< 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 does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
what is inflammation
Shelly Reply
part of a tissue or an organ being wounded or bruised.
Wilfred
what term is used to name and classify microorganisms?
Micheal Reply
Binomial nomenclature
adeolu
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