Does main() need to be inside a class in java syntax?


#1

In the java lesson about OOP, they put
public void main(){}
inside of a class, is this truly necessary? Or can you make a program without OOP if you so please?


#2

Do you mean

public static void main(String[] args){
    //statements
}

if so
In Java, main is a static method. This means the method is part of its class and not part of object.
When you execute a class with the JVM, the runtime environment starts by calling the class's main().
This main() method then calls all the other methods required to run your application.
This method accepts a single parameter which is an array of Strings. This parameter is the mechanism through which the jre passes command line arguments to your application

You don't necessarily have to include the main() method in your class or code but if you want to run your application you need that method


#3

In languages that compile to machine code, functions are just memory addresses, so calling such a function involves pushing arguments and the current location (so that execution can return to this location after) onto the stack and then telling the cpu to continue execution at the address of the function to be called.

(The stack is sort of like the working-memory of a program. For example, to return 5, one would push 5 to the stack and then return. Whoever is then waiting for a return value would then look at the top of the stack and find the 5 there)

In those languages, classes tend to be just an abstraction layer used while writing the code. The compiler does away with it for the final result.

Java isn't quite as raw, those classes exist during run-time and the code isn't executed directly by cpu, rather by a virtual machine (a separate program that is sort of like a giant while-true loop that executes one instruction after another) and everything in Java revolves around classes and the main method is no exception.

Why? Don't know, I can barely write hello world in Java.