<< 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 mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
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