Java Advanced Placement Study Guide:  The super Keyword, final Keyword, and static Methods

Sample question:  Can a static method directly access an instance variable of its own class?

Published: January 1,  2004
By Richard G. Baldwin

Questions

File JavaAP018.htm


Welcome

This is one in a miniseries of tutorial lessons designed to help you learn the essential features of Java object-oriented programming as identified by The College Board.

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.



1.  What output is produced by the following program?
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

Answer and Explanation

2.  What output is produced by the following program?

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

Answer and Explanation

3.  What output is produced by the following program?

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 

Answer and Explanation

4.  What output is produced by the following program?

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 

Answer and Explanation

5.  What output is produced by the following program?

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 

Answer and Explanation

6.  What output is produced by the following program?

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 

Answer and Explanation

7.  What output is produced by the following program?

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

Answer and Explanation

8.  What output is produced by the following program?

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

Answer and Explanation

9.  What output is produced by the following program?

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

Answer and Explanation

10.  What output is produced by the following program?

public 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

Answer and Explanation



Copyright 2002, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

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.

baldwin.richard@iname.com


What is Included?

According to the subset document,

Answers and Explanations

Answer 10

C.  38.48451000647496
    12.566370614359172

Back to Question 10

Explanation 10

Use 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 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{
  public static double area(double r){
    return r*r*Math.PI;
  }//end area()
   
}// end class

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.

Answer 9

C.  5

Back to Question 9

Explanation 9

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 class

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

Answer 8

A.  Compiler Error

Back to Question 8

Explanation 8

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

Answer 7

C.  A static method

Back to Question 7

Explanation 7

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

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

Answer 6

D.  3.1415927

Back to Question 6

Explanation 6

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, but the cast to type float reduces the precision down to 3.1415927

Answer 5

C.  5

Back to Question 5

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

Answer 4

A.  Compiler Error

Back to Question 4

Explanation 4

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

Answer 3

A.  Compiler Error

Back to Question 3

Explanation 3

The final keyword

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:

That leaves only final variables (constants).

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

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.

Answer 2

F.  20, 10

Back to Question 2

Explanation 2

Invoking a parameterized constructor

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{
  //...
  
  public Superclass(int x){
   
//...
  }//end constructor
  
  //...
}//end Superclass

class Subclass extends Superclass{
  //...
  
  public Subclass(){
    super(20);
    //...
  }//end constructor
  
  //...
}//end Subclass

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.

Answer 1

D.  5, 10

Back to Question 1

Explanation 1

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 Subclass

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{
  private int x = 1;
    
  public Superclass(){
    x = 5;
  }//end constructor
  
  //...
}//end 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.



Copyright 2002, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

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.

baldwin.richard@iname.com

-end-