r/javahelp • • Dec 15 '20

what does public static void main(String[] args) mean??

I know, i Know, i might be the most stupid person on this community, but i just started studying java 2 days ago and I'm seeing this everywhere. can someone please explain this in a language that dum dums like me can understand? thank you in advance.

19 Upvotes

13 comments sorted by

•

u/AutoModerator Dec 15 '20

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

15

u/irocgts Dec 15 '20

I like my start method to be: public static void main(String[] cheese)

then I try to remember to fix it before anyone sees it

2

u/TheAncientNoob_yt Dec 16 '20

haha! made me laugh in a topic thats soo hard for me!

4

u/mind_overflow Dec 15 '20

I'll try to explain it as noob-friendly as possible.

As you probably already know, Java programs are made of classes. Every program must have a "Main" class (you can name it however you want). This is the first class that will be loaded when your program is ran. However, the program must also know which method to execute, because it needs to know where the first instructions to run are (you wouldn't want it to run your code in a different order, would you?).

The Java team decided to name this method main. So, you can't name it "startup", "load", or whatever - it must be "main".

Also, this method needs a String[] array. Why is that? What is it? Let's assume you are running a program from a Terminal/CMD window. To create a directory, you would write:

mkdir DirectoryName

"DirectoryName" is a string that you are giving to the program named "mkdir", so that it knows what the folder name should be. Let's now assume you are running your compiled program from a Terminal window. You would write something like:

java -jar myProgram.jar Tony 2003

"Tony 2003" is exactly the String[] array we are talking about. It's used communicate with the program when starting it up. You're letting it know that you want "Tony 2003" to be the input, and your program will use it to do anything you want. For example, it might print a line saying "Hello! You are Tony, and you were born in 2003."

So, you main method needs a String[] array as a parameter: main (String[] args). We are naming it "args" because those are, indeed, the program arguments. You can give it any name you want however. You could name it arguments, a, lines, etc. The important is that there is a String array, not what it's named, since you'll be the only one to use it and you're the one who has to remember and use that name.

Now, what do public static void mean?

void means that your method is not returning a value. This method is the one that should be running your entire program, it's not a math function, a true/false check, or anything like that. If you were to calculate the square root of a number, you would declare a double method, such as:

double sqr(double number)
{
   return Math.sqrt(number);
   // ^ this methor RETURNS a DOUBLE value, so it needs a RETURN statement.
}

You don't want your main method to be returning any value however, as it's not simply doing one specific operation. At least, this is what the Java team believed when they decided that the main method must be a void. So, the main method must be a void one, or else, it won't be found.

static means that no matter if and when your Main class is instanced (it really shouldn't), the method will always be the same (identity). What does this mean? In Java, you can create instances (copies) of classes, that can hold different values, although the basic class is the very same one. For example:

public class Number
{
   private int number = 0;
   
   public void setNumber(int newValue)
   {
      number = newValue;
   }

   public int getNumber()
   {
      return number;
   }
}

This is a simple class that holds a generic number. You can set this number by calling setNumber(n); you can retrieve it by calling getNumber(). You can create multiple instances of this class, and all of them could be holding different numbers. Example:

Number aaa = new Number();
Number bbb = new Number();
// ^ you are creating two instances, named aaa and bbb.

aaa.setNumber(3);
bbb.setNumber(8);
// ^ you are assigning different values to those two instances' numbers.

System.out.println("AAA's Number: " + aaa.getNumber());
System.out.println("BBB's Number: " + bbb.getNumber());
// ^ you are printing the two different numbers.

By running this, your program would print:

AAA's number: 3
BBB's number: 8

As you can see, the class is the same: Number. However, the two instances are holding different numbers.

However, if we were to add a static value to those classes, it would be the same one, no matter the instance. Example:

public class Number
{
   private static int number = 0;
   
   public static void setNumber(int newValue)
   {
      number = newValue;
   }

   public static int getNumber()
   {
      return number;
   }
}

We made "number" static, so we also needed to make the two methods static (you must have static methods if you want to edit static variables). Let's try it now:

Number aaa = new Number();
Number bbb = new Number();
// ^ you are creating two instances, named aaa and bbb.

aaa.setNumber(3);
bbb.setNumber(8);
// ^ you are assigning different values to those two instances' numbers.
// note that we should instead be using Number.setNumber(n) instead of the instances, since this is a static method.

System.out.println("AAA's Number: " + aaa.getNumber());
System.out.println("BBB's Number: " + bbb.getNumber());
// ^ you are printing the two different numbers.

Your program will now print this:

AAA's number: 8
BBB's number: 8

This is because "number" is static, so when we ran bbb.setNumber(8), we actually set it for the CLASS, and not for the single instance. So, every instance now has 8 as a value.

We need the main method to be static because we are never going to instantiate the Main class, and this method must be a singleton. You wouldn't want two different main methods, right?

Let's now move on to public. Yay, this is easy! Public means that this method can be accessed from anywhere. From another program, from another class, from the class itself. This is mandatory because you are running your program from your filesystem, so the Java VM must be able to access it. If you were to make it private, only the class itself would be able to execute it. But how can the class itself execute it, if the program is not even started? You need to have it public so that you are able to run your program.

Hope this helps!

2

u/TheAncientNoob_yt Dec 16 '20

Thank you soooo much! you made me understand a concept which was so hard for me for so much time!

2

u/AlexMelillo Dec 15 '20

So I know you didn't ask for this, but I'm gonna give you some advice. Don't try to understand all the why's when you're starting out. There are more important things to focus on like learning to think in code (control flow). Early on, you simply have to accept that some things are just set that way and that's it.

I remember asking myself this very same question when I started with Java and I remember understanding what it actually meant when I had already been programming for about two months. This is something you will understand once you are taught object oriented programming part of Java.

With that said, I don't wanna leave you with no answers. So I will try to explain it to you in the simplest of terms.

Java programs can get extremely complex. They can be made up of hundreds of java files. The Java virtual machine needs to know which of these files is the one it needs to "execute" first in order to start your program. The way we tell the Java VM where to start reading is by giving one of these java files a main method. A method is just a function that is associated to a class. Since you don't know what a class is yet, don't worry about it. In all intents and purposes, a method is just a function. A simple block of code that gets executed when called upon.

Let's break down each part. "Public" means that this method can be called from anywhere. Static means we don't have to instantiate an object of whatever class the method belongs to in order to run it. Again, since you don't know what a class is. Don't worry about it for now. Void means that the "result" of this particular method isn't anything at all. This will make more sense when you learn about classes and methods. "main" is just the name of the method. When you eventually make your own methods, you will be able to call them whatever you want. The particular thing about the main method is that it needs to be called that way since that is the name the Java VM will be looking for when it runs your program.

Sorry for the wall of text and crappy formatting. I'm on my phone right now.

1

u/TheAncientNoob_yt Dec 16 '20

wow. you just explained it in the way i hoped someone would have explained this to me. Thank you!!

38

u/[deleted] Dec 15 '20 edited Dec 15 '20

Short answer

It's java entry point.

Basically, when you run a java program, it looks for this exact method to start everything else.

Edit. (Pressed the wrong button before finished)

Long answer.

At boot time the Java Virtual Machine (JVM) looks for a method named main that can be executed without an instance (static), this must be visible for everyone (public), must be able to receive the command line arguments as an array of strings (String[]), and must return nothing when invoked (void).

Don't worry if you don't understand some concepts like instance or visibility (I don't know if you're new to code or just new to java), you will (you should) learn those eventually.

9

u/morhp Professional Developer Dec 15 '20

It means you're declaring (writing down) a public, static method, that returns nothing (void) and takes a String-array as argument.

A method with this signature (combination of name and parameter types) is used by the Java virtual machine to start the program, it is the method that is first executed when you launch the program.

Don't worry if you don't understand everything completely, if this is just your second day.

6

u/steave435 Dec 15 '20

Yeah, it's completely normal for it to just be a magical incantation for a while. Normally, you learn these concepts slowly over time. You'll get the void and argument part when you start learning methods and public+static when you start learning about multi-file programs.

8

u/Migeil Dec 15 '20

If you're only two days in, forget about everything surrounding the word "main". That's all you need to focus on. It's called main because it's the main method of your program. It's the entry point of your program.

When you start your program, it starts reading lines from this method.

That's all you need to know as a beginner. The rest will become clear as you learn. That's a guarantee.

0

u/[deleted] Dec 15 '20

[deleted]

1

u/TheAncientNoob_yt Dec 16 '20

Thank you! ill check it out.