Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

Diego Murray
Diego Murray
2,515 Points

Parameters (String Prompt) in a method.

Below is part of some code. Can someone please explain what String prompt is. Is this native to java or created by the coder. How does the code execute?

class calc{
   public static void main(String[] args) {
     String s1 = getInput("Enter a numeric value: ");
     String s2 = getInput("Enter a second numeric value: ");
}

    private static String getInput(String prompt) {
        System.out.println(prompt);
        Scanner sc = new Scanner(System.in);
        return sc.nextLine();
    }
}

1 Answer

Simon Coates
Simon Coates
28,693 Points

prompt is passed to the getInput method, if you look at the main method.

String s1 = getInput("Enter a numeric value: ");

prompt is "Enter a numeric value: " for the that method execution. The main method on a class is how code executes. Java knows to look for a static method on a class with a public static void main(String[] args) signature. The main method directly or indirectly executes everything.

Diego Murray
Diego Murray
2,515 Points

Thanks for the ultra-quick response