Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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,694 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