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

calculate molarity of NaOH solution when 25.0ml of NaOH titrated with 27.2ml of 0.2m H2SO4
Gasin Reply
what's Thermochemistry
rhoda Reply
the study of the heat energy which is associated with chemical reactions
Kaddija
How was CH4 and o2 was able to produce (Co2)and (H2o
Edafe Reply
explain please
Victory
First twenty elements with their valences
Martine Reply
what is chemistry
asue Reply
what is atom
asue
what is the best way to define periodic table for jamb
Damilola Reply
what is the change of matter from one state to another
Elijah Reply
what is isolation of organic compounds
IKyernum Reply
what is atomic radius
ThankGod Reply
Read Chapter 6, section 5
Dr
Read Chapter 6, section 5
Kareem
Atomic radius is the radius of the atom and is also called the orbital radius
Kareem
atomic radius is the distance between the nucleus of an atom and its valence shell
Amos
Read Chapter 6, section 5
paulino
Bohr's model of the theory atom
Ayom Reply
is there a question?
Dr
when a gas is compressed why it becomes hot?
ATOMIC
It has no oxygen then
Goldyei
read the chapter on thermochemistry...the sections on "PV" work and the First Law of Thermodynamics should help..
Dr
Which element react with water
Mukthar Reply
Mgo
Ibeh
an increase in the pressure of a gas results in the decrease of its
Valentina Reply
definition of the periodic table
Cosmos Reply
What is the lkenes
Da Reply
what were atoms composed of?
Moses 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