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

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