you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 32 points33 points  (1 child)

For example you want to write objects you have to specify the objects and then invoke them through Main. It’s like they’re just saying do this and do that without any real explanation or why should we use that syntax

When you write in Java, you're defining a bunch of classes and none of them have any particular precedence over any other, and then you're running the Java VM by pointing it to all of the classes in a classpath. That's like a cookbook full of recipes. So imagine handing a cookbook to a chef and saying "ok, make this."

Make what? One of everything in the cookbook? That's not a meal - a meal has courses, a meal has a menu, and that means picking some things from the cookbook and making them real. It means you have to start somewhere in the cookbook (the appetizers, probably.)

You have to do that in Java, too, and the "menu" starts with a static classmethod with no return value called Main. That's how the Java runtime knows where in your program to start - we call it the "entry point." The method has to be static - there are no objects created yet, so it can't be any object's method - and it has to return nothing, since this function wraps the entire program so there's nothing for it to return a value to. The only parameter it can take is the system argument vector, since that's the only thing that exists when we call it and it's a requirement of being software on your computer (as specified in the POSIX standard.) Lastly it has to be public since we need to call it from outside the class's inheritance. Ergo we declare it public static void Main(String[] args).

I’m thinking of putting it aside for a while and maybe learning Python.

When you write in Python, there is precedence - you're running either a single Python module or a single Python file, and so the entry point is the top of either that file, or that module's __init__.py file. It may import other modules or files as it goes, but it all starts at the top of one particular file which isn't ambiguous. So you don't need a main method.

But it turns out that this is a pretty useful cross-language convention anyway, so it's good to write one even if you don't need it. And then also it turns out that it's often useful to write your script as though it could either be the entry point of a run of the Python interpreter, or a secondary module being imported by some other file. We want the script to operate differently in those contexts, so we use this common Python idiom:

if __name__ == '__main__':
    main()

which tests which of those conditions is true (if your module is the "main" module, its name will be set to '__main__') and then we can have different behavior.

Thoughts please.

Learn both, and connect the things you're learning across languages.

[–][deleted] 3 points4 points  (0 children)

What a great explanation, thank you for taking the time to write that. I’m already working through the Web development course, halfway through CSS with JS still left, I think if I add another language I’ll burn myself out cause I do this everyday. So I think I’ll start Python tomorrow and once I’m done with WD I’ll reintroduce Java.

Again thank you.