<< Chapter < Page Chapter >> Page >

Reverse the order of the variable declarations

The problem can be resolved by reversing the order of the two static variable declarations in the following revisedversion of the program.

public class Ap116{ public static void main(String args[]){System.out.println(Worker.twoPI); }//end main()}//end class Ap116 class Worker{public static final double myPI = Math.PI;public static final double twoPI = 2 * myPI;}// end class Worker

This revised version of the program compiles and executes successfully.

Back to Question 7

Answer 6

C. 6.283185307179586

Explanation 6

Access via an object

Question 5 illustrated the fact that a public static final member variable of a class can be accessed via a reference to an objectinstantiated from the class.

Not the only way to access a static variable

However, that is not the only way in which static member variables can be accessed. More importantly, public static member variables of a class can be accessed simply by referring to the name of the class and thename of the member variable joined by a period.

(Depending on other factors, it may not be necessary for the static variable to also be declared public , but that is the most general approach.)

A public static final member variable

In this program, the Worker class declares and initializes a public static final member variable named twoPI as shown in the following fragment.

class Worker{ public static final double twoPI= 2 * Math.PI; }// end class Worker

Accessing the static variable

The single statement in the main method of the controlling class accesses and displays the value of the public static final member variable named twoPI as shown in the following fragment.

public class Ap115{ public static void main(String args[]){System.out.println(Worker.twoPI); }//end main()}//end class Ap115

Objects share one copy of static variables

Basically, when a member variable is declared static , no matter how many objects are instantiated from a class (including no objects at all) , they all share a single copy of the variable.

Sharing can be dangerous

This sharing of a common variable leads to the same kind of problems that have plagued programs that use global variables for years. If the code in any object changes the value of the static variable, it is changed insofar as all objects are concerned.

Should you use non-final static variables?

Most authors will probably agree that in most cases, you probably should not use static variables unless you also make them final .

(There are some cases, such as counting the number of objects instantiated from a class, where a non-final static variable may be appropriate. However, the appropriate uses of non-final static variables are few and far between.)

Should you also make static variables public ?

If you make your variables static and final , you will often also want to make them public so that they are easy to access. There are numerous examples in the standard Java class libraries where variables aredeclared as public , static , and final . This is the mechanism by which the class libraries create constants and make them availablefor easy access on a widespread basis.

Questions & Answers

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
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
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