Let's start with the most important Java method. Each Java program should always have it. This method is an entry point of the program.
Syntax:
public static void main(String[] args) {
{
public
With the access modifier 'public' a method can be executed by any program. If the method would be non-public then there are some access restrictions that will be applied.
static
static does the main method is static that that's why JVM (Java Virtual Machine) can load class into memory and call this method.
void
In Java it is accepted that each method provides the return type. Since the main method does not return anything, adding 'void' prevents an error when the program is compiled.
main
This is the name of java main method.
String[] args
Java main method accepts a single argument of a type String array. All the command line arguments are stored in that array.
Next look at the method that outputs a string to console:
System.out.print();
or
System.out.println();
difference: after completing first - cursor left in the same line, when used println - cursor is moved to a new row.
Let's create first program:
package mypackage;
public class TestClass {
public static void main(String[] args) {
System.out.print("Hi! ");
System.out.println("It's our first program");
System.out.println();
System.out.println("Well done!");
}
}