Skip to content Skip to navigation

OpenStax_CNX

You are here: Home » Content » Ap0090: Self-assessment, the super keyword, final keyword, and static methods

Navigation

Recently Viewed

This feature requires Javascript to be enabled.
 

Ap0090: Self-assessment, the super keyword, final keyword, and static methods

Module by: R.G. (Dick) Baldwin. E-mail the author

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.

Preface

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.

Questions

Question 1 .

What output is produced by the program shown in Listing 1 ?

  • A. Compiler Error
  • B. Runtime Error
  • C. 1, 2
  • D. 5, 10
  • E. None of the above
1
Listing 1: Listing for Question 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

Answer and Explanation

Question 2

What output is produced by the program shown in Listing 2 ?

  • A. Compiler Error
  • B. Runtime Error
  • C. 1, 2
  • D. 5, 2
  • E. 5, 10
  • F. 20, 10
  • G. None of the above
2
Listing 2: Listing for Question 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

Answer and Explanation

Question 3

What output is produced by the program shown in Listing 3 ?

  • A. Compiler Error
  • B. Runtime Error
  • C. 5
  • D. None of the above
3
Listing 3: Listing for Question 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 

Answer and Explanation

Question 4

What output is produced by the program shown in Listing 4 ?

  • A. Compiler Error
  • B. Runtime Error
  • C. 5
  • D. None of the above
4
Listing 4: Listing for Question 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 

Answer and Explanation

Question 5

What output is produced by the program shown in Listing 5 ?

  • A. Compiler Error
  • B. Runtime Error
  • C. 5
  • D. None of the above
5
Listing 5: Listing for Question 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 

Answer and Explanation

Question 6

What output is produced by the program shown in Listing 6 ?

  • A. Compiler Error
  • B. Runtime Error
  • C. 3.141592653589793
  • D. 3.1415927
  • E. None of the above
6
Listing 6: Listing for Question 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 

Answer and Explanation

Question 7

What output is produced by the program shown in Listing 7 ?

  • A. Compiler Error
  • B. Runtime Error
  • C. A static method
  • D. None of the above
7
Listing 7: Listing for Question 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

Answer and Explanation

Question 8

What output is produced by the program shown in Listing 8 ?

  • A. Compiler Error
  • B. Runtime Error
  • C. 5
  • D. None of the above
8
Listing 8: Listing for Question 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

Answer and Explanation

Question 9

What output is produced by the program shown in Listing 9 ?

  • A. Compiler Error
  • B. Runtime Error
  • C. 5
  • D. None of the above
9
Listing 9: Listing for Question 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

Answer and Explanation

Question 10

Which output shown below is produced by the program shown in Listing 10 ?

Note:

A.  Compiler Error
B.  Runtime Error
C.  38.48451000647496
    12.566370614359172
D.  None of the above
10
Listing 10: Listing for Question 10.
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

Listings

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.

Miscellaneous

This section contains a variety of miscellaneous information.

Note:

Housekeeping material
  • Module name: Ap0090: Self-assessment, the super keyword, final keyword, and static methods
  • File: Ap0090.htm
  • Originally published: 2002
  • Published at cnx.org: 12/05/12
  • Revised: 01/02/13

Note:

Disclaimers:

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.

Answers

Answer 10

Note:

C.  38.48451000647496
    12.566370614359172

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

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

Note:

getInstance(int)

Static method in class java.awt.AlphaComposite

Creates an AlphaComposite object with the specified rule.

Back to Question 10

Answer 9

C. 5

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.

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

Back to Question 9

Answer 8

A. Compiler Error

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:

Note:
Ap107.java:17: non-static variable x
cannot be referenced from a static context

    System.out.println(x);

Back to Question 8

Answer 7

C. A static method

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

Note:
//...
        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 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.

Back to Question 7

Answer 6

D. 3.1415927

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

The cast to type float reduces the precision down to 3.1415927

Back to Question 6

Answer 5

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

Back to Question 5

Answer 4

A. Compiler Error

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?

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

Note:

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

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

Note:
Ap103.java:16: illegal start of
expression
    public final int x = 5;

Back to Question 4

Answer 3

A. Compiler Error

Explanation 3

The final keyword

The final keyword can be applied in a variety of ways in Java. This includes:

  • final parameters
  • final methods
  • final classes
  • 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 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.

Note:
    final int x = 5;
    x = 10;

A compiler error

JDK 1.3 produces the following error message:

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

Back to Question 3

Answer 2

F. 20, 10

Explanation 2

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.

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

Back to Question 2

Answer 1

D. 5, 10

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

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

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

Back to Question 1

-end-

Content actions

Download module as:

PDF | EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks