A method is a block of code that performs a specific task and can be called by other parts of a program to perform that task. Methods are used to encapsulate logic and promote code reuse, which makes programs easier to read, maintain, and modify.
A method is defined by specifying its name, return type, and parameter list. The return type specifies the data type of the value returned by the method, or void if the method does not return a value. The parameter list specifies the input values that the method expects, if any.
Parentheses are used when defining functions (or methods in Java) to declare the parameters that the function expects to receive. The parameters are placed inside the parentheses and separated by commas.
public class Main {
public static void simplePrinter() {
System.out.println("I am from simplePrinter method");
}
public static void multiCall() {
System.out.println("Results using multiCall method\n");
backupPrinter();
backupPrinter();
backupPrinter();
}
public static void main(String[] args) {
//simplePrinter();
//backupPrinter();
multiCall();
}
public static void backupPrinter() {
System.out.println("I am from backupPrinter method");
}
}
Here's a block-by-block explanation of the program:
public class Main {
This line defines the class named Main
.
public static void simplePrinter() {
System.out.println("I am from simplePrinter method");
}
This line defines a static method named simplePrinter()
, which simply prints a message to the console.
public static void multiCall() {
System.out.println("Results using multiCall method\n");
backupPrinter();
backupPrinter();
backupPrinter();
}
This line defines another static method named multiCall()
, which prints a message to the console and then calls the backupPrinter()
method three times.
public static void main(String[] args) {
multiCall();
}
This line defines the main()
method, which is the entry point for the program. It simply calls the multiCall()
method.
public static void backupPrinter() {
System.out.println("I am from backupPrinter method");
}
}
This line defines the backupPrinter()
method, which prints a message to the console.
When the program runs, the main()
method is called, which in turn calls the multiCall()
method. The multiCall()
method prints a message and then calls the backupPrinter()
method three times, each time printing a message to the console.
What "public static void" means
In Java, public static void
are access modifiers and a return type that can be used to define methods. Here's what each of these keywords means:
public
: This is an access modifier that means the method can be accessed from anywhere within the program. Other access modifiers includeprivate
,protected
, and the default (no modifier).static
: This keyword indicates that the method belongs to the class itself, rather than an instance of the class. This means that the method can be called without creating an instance of the class.void
: This is the return type of the method, which means the method does not return any value. If the method were to return a value, the return type would be replaced with the type of the value being returned.
So in the context of a method signature like public static void methodName()
, it means that the method is accessible from anywhere in the program, belongs to the class itself, and does not return any value.
No comments:
Post a Comment