methods... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

methods...

how does the second method know that david or amy = name??

11th Jul 2016, 12:47 PM
Antonio Hu
4 Answers
+ 2
the method static void sayHello(String name) is a method with an argument, String name. What this means, is that when the method is called in the main method, sayHello("David"); the David string is being put into the argument name. So essentially, name becomes "David". The method is called again with sayHello("Amy"); This time, name becomes the string "Amy". Method sayHello just prints out hello with the value of String name at the time it is called. If you were to try to call the sayHello(); with no name, you'd get an error because you're not passing a value for the argument.
11th Jul 2016, 1:45 PM
James
James - avatar
+ 1
class MyClass { static void sayHello(String name) { System.out.println("Hello " + name); } public static void main(String[ ] args) { sayHello("David"); sayHello("Amy"); } }
11th Jul 2016, 1:36 PM
Antonio Hu
0
my bad i thought the questions were divided for each "page"
11th Jul 2016, 1:37 PM
Antonio Hu
0
"bavid"
12th Aug 2016, 10:05 AM
Sanjay Pusala
Sanjay Pusala - avatar