java / beginner
Snippet
Defining and Calling Methods
Methods are reusable blocks of code. 'void' means the method does not return a value, and parameters allow you to pass data into the method.
snippet.java
1
2
3
4
5
6
public static void greet(String user) {System.out.println("Hello, " + user);}// Calling the methodgreet("Alice");
Breakdown
1
public static void greet(String user)
Defines a public method named 'greet' that takes a String parameter.
2
greet("Alice");
Invokes the method and passes the literal string 'Alice' as the argument.