public static void main if for eating?
For those who are just starting, I believe one of the main things that “scares” (in the sense of running away in fear) people is the syntax of the main method, which can be somewhat complex and strange.
But here we go: the first piece of advice I give and always tell my students is:
Consider initially that the main method is just a necessary header for your application. That’s it!
Whether you like it or not, this takes away some weight and responsibility from you in terms of trying to understand everything that happens in the header right away. But of course, our main goal in this article is not to reduce the complexity of the header to a simple “let’s ignore it.” Let’s understand the whole dynamic of it.
Let’s take a close look at the header and break it down step by step:
public static void main(String args[]) { ... }
First of all, we need to understand that main is not a method called explicitly. In the execution of your program, you don’t type the command java MyClassName.main. This is a method that the Java Virtual Machine needs to call. Since it’s the JVM that calls it, it’s only fair that we follow its specification. So here we go.
Understanding each part:
– public– the main method needs to be public, which means the Java Virtual Machine needs to be able to access it, regardless of where your class is located (or the package it’s in).
– static – the method must be static, meaning there’s only one instance of it and it’s free from class instances. What does this mean? The Java Virtual Machine doesn’t need to instantiate an object of your class’s type. It simply locates the main method and executes it (statically).
– void – it means there’s no return (or void return). In other words, your program, unlike those familiar with C or C++, doesn’t return any value to the Virtual Machine. The VM already monitors the execution of your program line by line (remember that the JVM is an interpreter for bytecodes), so it’s not necessary for your code to return anything.
– main – of course, the name of the main method (as translated).
– String args[] – here comes the big Achilles’ heel for those who are just starting, because if you want to understand, you’ll need to know about arrays and strings without ever having written a “Hello World” program. Do you now understand why I always tell my beginner students to consider it as just a header initially?
So let’s talk about the parameter. There’s a possibility that you can invoke your main program by passing command-line arguments, meaning when you type the command:
java YourClassName
It’s possible to pass parameters after the class name, separated by spaces, like this:
java YourClassName parameter1 parameter2 abc 123
This list (which we declare as args in the main method, but it could be any other name) contains all the parameters you passed.
And how do you access these parameters? Through their position in the array, like this:
java YourClassName parameter1 parameter2 abc 123 args[0] args[1] args[2] args[3]
There you go! The args array, like any other array, always starts at position 0 and goes up to args.length-1. In this case, our size is 4, and the positions range from 0 to 3. These values can already be considered inputs to your program.
Remember:
– If you want to pass numerical values, you must convert them (using Integer.parseInt, Float.parseFloat or Double.parseDouble).
– If your code accesses this array at a certain position, and you don’t provide anything when executing your program, it will throw an ArrayIndexOutOfBoundsException exception (index of the array out of bounds), indicating that you’re trying to access a position in an array that doesn’t have elements.
Could I help you? If you want, share this with your friends!