Published: January 1, 2004
By Richard G. Baldwin
Purpose
The purpose of this miniseries is to help you study for the Advanced Placement Examinations designed by the College Board.
Once you understand everything in this miniseries, plus the material in the lessons that I published earlier on Java Data Structures, you should understand the Java programming features that the College Board considers essential for the first two semesters of object-oriented programming education at the university level.
Approach
These lessons provide questions, answers, and explanations designed to help you to understand the subset of Java features covered by the Java Advanced Placement Examinations (as of October, 2001).
Please see the first lesson in the miniseries entitled Java Advanced Placement Study Guide: Introduction to the Lessons, Primitive Types, for additional background information. The previous lesson was entitled Java Advanced Placement Study Guide: Classes, Constructors, Setter Methods, and Getter Methods.
Supplementary material
In addition to the material in these lessons, I recommend that you also study the other lessons in my extensive collection of online Java tutorials, which are designed from a more conventional textbook approach. You will find those lessons published at Gamelan.com. However, as of the date of this writing, Gamelan doesn't maintain a consolidated index of my Java tutorial lessons, and sometimes they are difficult to locate there. You will find a consolidated index at Baldwin's Java Programming Tutorials.
What is Included?
Click here for a preview of the
Java
programming features covered by this lesson.
public class Ap100{ |
2. What output is produced by the following program?
public class Ap101{ |
3. What output is produced by the following program?
public class Ap102{ |
4. What output is produced by the following program?
public class Ap103{ |
5. What output is produced by the following program?
public class Ap104{ |
6. What output is produced by the following program?
public class Ap105{ |
7. What output is produced by the following program?
public class Ap106{ |
8. What output is produced by the following program?
public class Ap107{ |
9. What output is produced by the following program?
public class Ap108{ |
10. What output is produced by the following program?
public class Ap109{ |
Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.
Good object-oriented design dictates that static methods be used sparingly, and only in those situations where they are appropriate. As you might guess, not all authors will agree on the issue of appropriateness in all cases.
Is this an appropriate use of a static method?
However, I believe that most authors will agree that this program illustrates an appropriate use of a static method.
No persistence requirement
This static method computes and returns a result on a nonpersistent basis. That is to say, there is no attempt by the static method to save any historical information from one invocation of the method to the next. (Of course, the method that invokes the static method can save whatever it chooses to save.)
Avoiding wasted computer resources
In situations such as this, it would often be a waste of computer resources to require a program to instantiate an object and invoke an instance method on that object just to be able to delegate a nonpersistent computation to that method. (This is just about as close to a global method as you can get in Java.)
Computing the area of a circle
In this program, the Worker class
provides
a static method named area() that receives a double
parameter representing the radius of a circle. It computes and
returns
the area of the circle as a double value. The static
method named area is shown in the following code fragment.
class Worker{ |
As a driver, the main() method of the controlling class invokes the area() method twice in succession, passing different values for the radius of a circle. In each case, the main() method receives and displays the value that is returned by the area() method representing the area of a circle.
Static methods in the class libraries
If you examine the Java API documentation carefully, you will find numerous examples of static methods that produce and return something on a nonpersistent basis. (Again, nonpersistent in this case means that no attempt is made by the static method to store any historical information. It does a job, forgets it, and goes on to the next job when it is invoked again.)
Factory methods
For example, the alphabetical index of the JDK 1.3 API lists several dozen static methods named getInstance(), which are defined in different classes. These methods, which usually produce and return a reference to an object, are often called factory methods.
Here is the text from the API documentation
describing
one of them:
getInstance(int)
Static method in class java.awt.AlphaComposite Creates an AlphaComposite object with the specified rule. |
This program illustrates a rather convoluted
methodology
by which a static method can gain access to an instance member
of
an object.
class Worker{ |
In this example, the static method invokes a getter method on a reference to an object to gain access to an instance variable belonging to that object. This is what I meant in the discussion of the previous program when I said "going through a reference to an object of the class."
A static method cannot access non-static or instance members of its class without going through a reference to an object of the class.
In this program, the static method attempts to directly access the instance variable named x. As a result, JDK 1.3 produces the following compiler error:
Ap107.java:17: non-static variable x
cannot
be referenced from a static context
System.out.println(x);
This is a very straightforward example of the use of a static method.
When a method is declared static, it is not necessary to instantiate an object of the class containing the method in order to access the method (although it is possible to do so). All that is necessary to access a public static method is to refer to the name of the class in which it is defined and the name of the method joined by a period.
(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.
//... |
The class named Worker defines a public static method named staticMethod(). A statement in the main() method of the controlling class invokes the method by referring to the name of the class 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 invocation of the 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 time it is invoked without the requirement for data to persist between invocations.
The Math class contains many good examples of the use of static methods, such as abs, acos, asin, etc..
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 class and 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, but the cast to type float reduces the precision down to 3.1415927
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.
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 statement caused it, do you know what caused
it?
public final int x = 5; |
Using public static final member variables
As I mentioned earlier, 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:
public static final double PI
The double value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter. |
The constant named PI
You may recognize the constant named PI from your high school geometry class.
Whenever you need the value for the constant PI, you shouldn't have to instantiate an object just to get access to it. Furthermore, your class should not be required to have any special package relationship with the Math class just to get access to PI.
The good news ...
Because PI is declared to be both public and static in the Math class, it is readily available to any code in any method in any Java program that has access to the standard Java class library.
How is PI accessed?
PI can be accessed by using an expression as
simple
as that highlighted in boldface below, which consists simply of the
name
of the class and the name of the variable joined by a period.
double piRSquare = Math.PI * R * R; |
No notion of public local variables
As a result of the above, many of you may have become accustomed to associating the keyword public with the keyword final. However, if you missed this question and you have read the explanation to this point, you must also remember that there is no notion of public or private for local variables. Therefore, when this program was compiled under JDK 1.3, a compiler error was produced. That compiler error is partially reproduced below:
Ap103.java:16: illegal start of
expression
public final int x
= 5;
The final keyword can be applied in a variety of ways in Java. However, according to the subset document, the following are not tested by the AP CS exam:
Behaves like a constant
When the final keyword is applied to a variable in Java, that causes the variable to behave like a constant. In other words, the value of the variable must be initialized when it is declared, and it cannot be changed thereafter (see the exception discussed below).
Apply to local or member variables
The final keyword can be applied to either local variables or member variables. (In case you have forgotten, local variables are declared inside a method, while member variables are declared inside a class, but outside a method.)
So, what is the problem?
The problem with this program is
straightforward.
As shown in the following code fragment, after declaring a final
local variable and initializing its value to 5, the program attempts
to change the value stored in that variable to 10. This is
not
allowed.
final int x = 5; |
A compiler error
JDK 1.3 produces the following error message:
Ap102.java:17: cannot assign a value to
final
variable x
x = 10;
A very interesting twist
An interesting twist of the use of the final keyword with local variables is discussed below.
Background information
Regardless of whether or not the local variable is declared final, the compiler will not allow you to access the value in a local variable if that variable doesn't contain a value. This means that you must always either initialize a local variable or assign a value to it before you can access it.
So, what is the twist?
Unlike final member variables of a class, the Java compiler and runtime system do not require you to initialize a final local variable when you declare it. Rather, you can wait and assign a value to it later. However, once you have assigned a value to a final local variable, you cannot change that value later.
The bottom line
Whether you initialize the final local variable when you declare it, or assign a value to it later, the result is the same. It behaves as a constant. The difference is that if you don't initialize it when you declare it, you cannot access it until after you assign a value to it.
This is a relatively straightforward implementation of the use of the super keyword in a subclass constructor to invoke a parameterized constructor in the superclass.
The interesting code in the program is highlighted in the following
fragment. Note that quite a lot of code was deleted from the
fragment
for brevity.
class Superclass{ |
Using the super keyword
The code that is of interest is the use of super(20) as the first executable statement in the subclass constructor to invoke the parameterized constructor in the superclass, passing a value of 20 as a parameter to the parameterized constructor.
Note that when the super keyword is used in this fashion in a constructor, it must be the first executable statement in the constructor.As before, the program plays around a little with initial values for instance variables to see if you are alert, but the code that is really of interest is highlighted in the above fragment.
The purpose of this question and the associated answer is to illustrate explicitly what happens automatically by default regarding the execution of constructors.
The Subclass constructor
This program defines a class named Subclass, which extends a
class named Superclass. A portion of the Subclass
definition,
including its noarg constructor is shown in the following code
fragment.
(The
class also defines a getter method, which was omitted here for brevity.)
class Subclass extends Superclass{ |
The super keyword
The important thing to note in the above fragment is the boldface statement containing the keyword super.
The super keyword has several uses in Java. As you might guess from the word, all of those uses have something to do with the superclass of the class in which the keyword is used.
Invoke the superclass constructor
When the super keyword (followed by a pair of matching parentheses) appears as the first executable statement in a constructor, this is an instruction to the runtime system to first invoke the constructor for the superclass, and then come back and finish executing the code in the constructor for the class to which the constructor belongs.
Invoke the noarg superclass constructor
If the parentheses following the super keyword are empty, this is an instruction to invoke the noarg constructor for the superclass.
Invoke a parameterized superclass constructor
If the parentheses are not empty, this is an instruction to find and invoke a parameterized constructor in the superclass whose formal arguments match the parameters in the parentheses.
Invoke the noarg superclass constructor by default
Here is an important point that is not illustrated above:
If the first executable statement in your constructor is not an instruction to invoke the constructor for the superclass, an instruction to invoke the noarg constructor for the superclass will be effectively inserted into your constructor code before it is compiled.Therefore, a constructor for the superclass is always invoked before the code in the constructor for your new class is executed.
You can choose the superclass constructor
The superclass constructor that is invoked may be the noarg constructor for the superclass, or you can force it to be a parameterized constructor by inserting something like super(3,x,4.5); as the first instruction in your constructor definition.
Always have a noarg constructor ...
Now you should understand why I told you in an earlier lesson that the classes you define should almost always have a noarg constructor, either the default noarg version, or a noarg version of your own design.
If your classes don't have a noarg constructor, then anyone who extends your classes will be required to put code in the constructor for their new class to invoke a parameterized constructor in your class.
In this program, the super(); statement in the Subclass
constructor causes the noarg constructor for the Superclass
to be invoked. That noarg constructor is shown in
boldface
in the following code fragment.
class Superclass{ |
Additional code
Beyond an exposure and explanation of the use of the super keyword to invoke the superclass constructor, this program plays a few games with initial values of instance variables just to see if you are alert to that sort of thing. However, none of that should be new to you, so I won't discuss it further here.
Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.
-end-