class class_name {
output input
modifiers return_type method_name (parameter_list) {
...
class
method
{...} block
data_type name = value; variable
}
}
Main Method
When the JVM starts running, it looks for the class you give it at the command line. Then it starts looking
for a specially-written method(main method) that looks exactly like:
public static void main (String[] args) { (shortcut: psvm → tab)
// your code goes here
}
Next, the JVM runs everything between the curly braces { } of your main method.
★ Every Java application has to have at least one class, and at least one main method (not one main per
class; just one main per application).
Method Calling
class A {
public static void main (String[] args) {
System.out.println("Hello! main method"); (shortcut: sout→ tab)
// A.method1(); (using the class flag)
method1();
} method calling
public static void method1 () {
System.out.println("Hello! method 1");
}
}
Only static methods can be called without creating an object, and this can be done in two ways:
• Directly within the same class: If the method is static and belongs to the same class where it's
being called, you can simply use the method name (e.g. method1();).
• Using the class name (class flag): If you are calling a static method from outside the class or for
explicit clarity, you can use the class flag followed by the method name (e.g. A.method1();).
nabiii_11 Page 1
, Single-threaded Execution Flow OUTPUT:
1. Hello! main method
class A { Hello! method 1
2. Hello! main method
public static void main (String[] args) {
System.out.println("1. Hello! main method");
A.method1();
System.out.println("2. Hello! main method");
}
public static void method1 () {
System.out.println("Hello! method 1");
}
}
One Thread, One Path
The program starts in the main method, proceeds through its statements, calls method1, executes method1,
and then returns to finish main. This is a linear, uninterrupted flow within a single thread.
method1 does not run in parallel with main; it runs within the same thread, temporarily interrupting the
execution of main.
Each line of code is executed strictly in order. System.out.println("1. Hello! main method"); must
finish before A.method1(); is called, and A.method1(); must complete before
System.out.println("2. Hello! main method"); can execute.
nabiii_11 Page 2