HIDE NAV

Greeter with Methods

The below command line program is a remake of the greeter demonstrating use of a static method.
To share data between methods parameters and returns are used.
Another example of static methods sharing data through parameter pass and return as demostrated in the Cube-This with Methods demo code.

Main.java


package ceccs;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        String fName = askQuestion("enter first name:");
        String lName = askQuestion("enter last name:");
        printGreeting(lName, fName);
    }


    /**decare a method with two string type parameters and no return data */
    static void printGreeting(String lastName, String firstName){
        System.out.println("Hello " + firstName + " " + lastName + ", Nice to meet you!");
    }

    /**decare a method with one string type parameter and a string return */
    static String askQuestion(String qText){
        System.out.println(qText);
        Scanner scan = new Scanner(System.in);
        String input = scan.nextLine();
        return input;
    }
}