Summary: Part of a self-assessment test designed to help you determine how much you know about the super keyword, the final keyword, and static methods in Java.
Note: You are viewing an old version of this document. The latest version is available here.
This module is part of a self-assessment test designed to help you determine how much you know about object-oriented programming using Java.
The test consists of a series of questions with answers and explanations of the answers.
The questions and the answers are connected by hyperlinks to make it easy for you to navigate from the question to the answer and back.
I recommend that you open another copy of this document in a separate browser window and use the links to under Listings to easily find and view the listings while you are reading about them.
What output is produced by the program shown in Listing 1 ?
public class Ap100{
public static void main(
String args[]){
new Worker().makeObj();
}//end main()
}//end class definition
class Worker{
public void makeObj(){
Subclass obj = new Subclass();
System.out.println(obj.getX() +
", " + obj.getY());
}//end makeObj()
}// end class
class Superclass{
private int x = 1;
public Superclass(){
x = 5;
}//end constructor
public int getX(){
return x;
}//end getX()
}//end Superclass
class Subclass extends Superclass{
private int y = 2;
public Subclass(){
super();
y = 10;
}//end constructor
public int getY(){
return y;
}//end getY()
}//end Subclass
What output is produced by the program shown in Listing 2 ?
public class Ap101{
public static void main(
String args[]){
new Worker().makeObj();
}//end main()
}//end class definition
class Worker{
public void makeObj(){
Subclass obj = new Subclass();
System.out.println(obj.getX() +
", " + obj.getY());
}//end makeObj()
}// end class
class Superclass{
private int x = 1;
public Superclass(){
x = 5;
}//end constructor
public Superclass(int x){
this.x = x;
}//end constructor
public int getX(){
return x;
}//end getX()
}//end Superclass
class Subclass extends Superclass{
private int y = 2;
public Subclass(){
super(20);
y = 10;
}//end constructor
public int getY(){
return y;
}//end getY()
}//end Subclass
What output is produced by the program shown in Listing 3 ?
public class Ap102{
public static void main(
String args[]){
new Worker().finalStuff();
}//end main()
}//end class definition
class Worker{
public void finalStuff(){
final int x = 5;
x = 10;
System.out.println(x);
}//end finalStuff()
}// end class
What output is produced by the program shown in Listing 4 ?
public class Ap103{
public static void main(
String args[]){
new Worker().finalStuff();
}//end main()
}//end class definition
class Worker{
public void finalStuff(){
public final int x = 5;
System.out.println(x);
}//end finalStuff()
}// end class
What output is produced by the program shown in Listing 5 ?
public class Ap104{
public static void main(
String args[]){
new Worker().finalStuff();
}//end main()
}//end class definition
class Worker{
void finalStuff(){
final int x = 5;
System.out.println(x);
}//end finalStuff()
}// end class
What output is produced by the program shown in Listing 6 ?
public class Ap105{
public static void main(
String args[]){
System.out.println(Worker.fPi);
}//end main()
}//end class definition
class Worker{
public static final float fPi =
(float)Math.PI;
}// end class
What output is produced by the program shown in Listing 7 ?
public class Ap106{
public static void main(
String args[]){
Worker.staticMethod();
}//end main()
}//end class definition
class Worker{
public static void staticMethod(){
System.out.println(
"A static method");
}//end staticMethod()
}// end class
What output is produced by the program shown in Listing 8 ?
public class Ap107{
public static void main(
String args[]){
Worker.staticMethod();
}//end main()
}//end class Ap107
class Worker{
private int x = 5;
public static void staticMethod(){
System.out.println(x);
}//end staticMethod()
}// end class
What output is produced by the program shown in Listing 9 ?
public class Ap108{
public static void main(
String args[]){
Worker.staticMethod();
}//end main()
}//end class Ap108
class Worker{
private int x = 5;
public static void staticMethod(){
System.out.println(
new Worker().getX());
}//end staticMethod()
public int getX(){
return x;
}//end getX()
}// end class
Which output shown below is produced by the program shown in Listing 10 ?
A. Compiler Error
B. Runtime Error
C. 38.48451000647496
12.566370614359172
D. None of the abovepublic class Ap109{
public static void main(String args[]){
System.out.println(Worker.area(3.5));
System.out.println(Worker.area(2.0));
System.out.println();
}//end main()
}//end class Ap109
class Worker{
public static double area(double r){
return r*r*Math.PI;
}//end area()
}// end class
I recommend that you open another copy of this document in a separate browser window and use the following links to easily find and view the listings while you are reading about them.
This section contains a variety of miscellaneous information.
Financial : Although the Connexions site makes it possible for you to download a PDF file for this module at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should be aware that some of the HTML elements in this module may not translate well into PDF.
I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. I neither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it was made and published without my prior knowledge.
Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.
C. 38.48451000647496
12.566370614359172Use static methods sparingly
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 non-persistent basis. That is to say, there is no attempt by the static method to save any historical information from one call of the method to the next. (Of course, the method that calls 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 call an instance method on that object just to be able to delegate a non-persistent 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{
public static double area(double r){
return r*r*Math.PI;
}//end area()
}// end classAs a driver, the main method of the controlling class calls 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 non-persistent basis. (Again, non-persistent in this context 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 called 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.
C. 5
Going through a reference to ...
This program illustrates a rather convoluted methodology by which a static method can gain access to an instance member of an object.
class Worker{
private int x = 5;
public static void staticMethod(){
System.out.println(
new Worker().getX());
}//end staticMethod()
public int getX(){
return x;
}//end getX()
}// end classIn this example, the static method calls 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 in the previous question when I said "going through a reference to an object of the class."
A. Compiler Error
A static method cannot access ...
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);
C. A static method
Using a static method
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 unless the class is declared abstract) . 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.
//...
Worker.staticMethod();
//...
class Worker{
public static void staticMethod(){
//...
}//end staticMethod()
}// end classThe 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 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 call 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 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.
D. 3.1415927
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 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
The cast to type float reduces the precision down to 3.1415927
C. 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.
A. Compiler Error
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 statement caused 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:
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 shown below, which consists simply of the name of the class and the name of the variable joined by a period (Math.PI) .
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;
A. Compiler Error
The final keyword
The final keyword can be applied in a variety of ways in Java. This includes:
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 or constructor, 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;
x = 10;A compiler error
JDK 1.3 produces the following error message:
Ap102.java:17: cannot assign a value to
final
variable x x = 10;An interesting twist - blank finals
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. (Some authors refer to this as a blank final.) 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.
F. 20, 10
Calling a parameterized constructor
This is a relatively straightforward implementation of the use of the super keyword in a subclass constructor to call 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{
//...
public Superclass(int x){
//...
}//end constructor
//...
}//end Superclass
class Subclass extends Superclass{
//...
public Subclass(){
super(20);
//...
}//end constructor
//...
}//end SubclassUsing 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 call 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.
D. 5, 10
The execution of constructors
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{
private int y = 2;
public Subclass(){
super();
y = 10;
}//end constructor
//...
}//end SubclassThe super keyword
The important thing to note in the above fragment is the 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 call 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.
Call the noarg superclass constructor
If the parentheses following the super keyword are empty, this is an instruction to call the noarg constructor for the superclass.
Invoke a parameterized superclass constructor
If the parentheses are not empty, this is an instruction to find and call 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 call the constructor for the superclass, an instruction to call the noarg constructor for the superclass will effectively be inserted into your constructor code before it is compiled.
Therefore, a constructor for the superclass is always called before the code in the constructor for your new class is executed.
You can choose the superclass constructor
The superclass constructor that is called 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 module 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 call 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 called. That noarg constructor is shown in the following code fragment.
class Superclass{
private int x = 1;
public Superclass(){
x = 5;
}//end constructor
//...
}//end SuperclassAdditional code
Beyond an exposure and explanation of the use of the super keyword to call 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.
-end-