콘텐츠로 이동

Unit 1.10: Calling Class Methods

Scope: CS Awesome 2, Section 1.10

Learning Goals

By the end of this lesson, you should be able to:

  1. Recognize a class method by the static keyword.
  2. Call a class method from the same or a different class.
  3. Distinguish void and non-void methods.
  4. Use a returned value in an assignment or expression.
  5. Diagnose argument and return-type mismatches.

Class Methods

A class method is associated with the class rather than with one particular object. Its header contains static before the return type.

public static int square(int number)
{
    return number * number;
}

When the call is inside the defining class, the class name is optional:

int area = square(6);

From another class, use the class name and dot operator:

int area = NumberTools.square(6);

Void Methods

A void method performs an action and does not return a value.

public static void printBorder()
{
    System.out.println("================");
}

Call it as a standalone statement:

printBorder();

This is invalid because there is no returned value to store:

String result = printBorder();

Non-Void Methods

A non-void method calculates and returns a value. Its declared return type states what type must be returned.

public static double average(double a, double b)
{
    return (a + b) / 2;
}

The returned value can be:

  • stored in a compatible variable;
  • printed;
  • used inside a larger expression;
  • passed as an argument to another compatible method.
double result = average(4.0, 7.0);
System.out.println(average(10.0, 20.0));
double adjusted = average(2.0, 8.0) + 1.5;

Calling a non-void method and ignoring its result is legal in many cases, but it usually loses the purpose of the calculation.

average(4.0, 7.0); // returned value is discarded

The return Statement

return expression;

The expression must be compatible with the method's return type. Executing return ends the current method and sends the value to the caller.

public static int lastDigit(int number)
{
    return number % 10;
}

Matching a Call to a Method

Given:

public static double ratio(int total, double units)

A correct call must respect the signature and return type:

double value = ratio(15, 4.0);

Common errors:

int value = ratio(15, 4.0);     // double return cannot be stored in int
double value = ratio(15.0, 4);  // first argument cannot narrow to int
double value = ratio(4.0);      // wrong number of arguments

An int argument may widen to a double parameter, so ratio(15, 4) is compatible.

Complete Example

public class UnitConverter
{
    public static double kilometersToMiles(double km)
    {
        return km * 0.621371;
    }

    public static void printLabel()
    {
        System.out.println("Converted distance:");
    }

    public static void main(String[] args)
    {
        printLabel();
        double miles = kilometersToMiles(5.0);
        System.out.println(miles);
    }
}

Practice Missions

Mission 1: Void or Non-Void?

For each task, decide whether the method should be void or non-void and propose a return type if needed:

  1. print a receipt heading;
  2. calculate the area of a rectangle;
  3. display an error message;
  4. determine the last digit of an integer.

Mission 2: Repair the Calls

Given these methods, correct only the calls and receiving variables.

public static int cube(int n) { return n * n * n; }
public static double divide(double x, double y) { return x / y; }
public static void announce(String text) { System.out.println(text); }
double a = cube(3.0);
int b = divide(9, 2);
String c = announce("Done");

Mission 3: Class-Qualified Calls

Assume TemperatureTools defines public static double toCelsius(double fahrenheit). Write a call from a different class, save the result, and print it. Explain why the class name is needed.

Mission 4: Returned-Value Pipeline

Write two class methods: doubleToInt(int n), which returns twice n, and lastDigit(int n). In main, pass the return value of the first call directly into the second call and print the final value.

Key Summary

Concept Core idea
Class method Associated with a class and marked static.
Same-class call Class name is optional.
Other-class call Usually ClassName.method(arguments).
Void method Performs an action without returning a value.
Non-void method Returns a value of its declared type.
Returned value Store it or use it in an expression.

Source scope: CS Awesome 2, Unit 1.10