Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

why does the args is required.

Hi, i have a little bit of experience with programming language, i used to developed some nice little games with c++ and c that worked on the cmd, and now i am learning java which is not so different from c++ as an OOP language, so it's going me pretty good, but i do have 1 question. i tried to run some code on my computer with the compiler javac, it didn't worked until i wrote the "String[] args" as a parameter in the main method. i know what an array is and i know how to use it and for what. but i don't understand why it is absolutely necessary that the main method will have a parameter passed in even if not really used, that is something i didn't have to do in c++ or c, i would like an explanation on why i can't just write main like this: "public static void main() { //some code... }"

4 Answers

Markus Ylisiurunen
Markus Ylisiurunen
15,034 Points

I'm not familiar with any C variation so I really have no idea how you'd do the same in C++. I'm not a pro in Java either but I'll try to explain it better this time. And all that I say may be completely wrong, this is just how I understand and think about it :)

So first of all the main method is a public static method of your class. I'm sure you know what it means since you've worked with OOP before. Whenever you run any Java program this static main method will execute first.

So what about the arguments you might ask. Let's imagine for a moment that we have a main method which accepts arguments as an array of strings. When you invoke this main method you'll need to pass it something or it won't run.

Situation 1: We run our program with one argume main method will get a array which has a length of 1.

Situation 2: We run our program with no arguments and main method will get an empty array.

Java has to pass something because there might be some arguments in the command that runs our program. You acutally don't invoke the main method yourself but Java will. It can't just sometimes pass nothing and sometime something else. It would cause an error.

Okay so now it should be clear that whenever we run our program, Java runtime (or whatever it's actually called) will executing our program starting from the main method. Java runtime (I'll just call it runtime) can't know if we have or don't have our main method accepting arguments so even though you don't pass any arguments at this point Java will still pass an empty array since this is the default behavior.

So what happens when we have a main method with no arguments. I'm not actually sure if it'll even compile but let's assume it will.

So we run our program with no arguments but Java will still create an empty array which it'll pass to main method. This is because it expects that we have our main method accepting arguments. And this will of course crash because we aren't allowing any arguments to be passed.

I'm sorry if this doesn't answer to your question but at least I tried my best to explain it :)

Markus Ylisiurunen
Markus Ylisiurunen
15,034 Points

Hi Ben!

I'm not 100% sure but I think it has to do with just how Java is implemented. Every Java program starts from the main method. If you'd define main without any parameters then there can never be any arguments passed to the main method when it's ran.

Think about when Java calls a main method with no arguments. If the main method which is called has any arguments, you can't call it without passing something. So when there's no arguments passed and the program is ran, Java will just pass empty String array.

Your case is that you'd want to run main method with no parameters. How could Java know if you really wanted to pass empty argument array from the command line (pass something another time) or pass simply nothing? There's no way to know this so it just expects that there will always be a way to pass something if needed.

I'm sorry if this wasn't clear but hopefully it helps at least a little :)

Thanks a lot for the answer Markus but i still don't understand it yet. i will try to explain why. In c++ for example i don't pass any argument. so what is the different that programs written in java needs to have that while in c++ you can write the main function just as simple as with no parameter at all. If it may help, what i understood from what you wrote is that it may happen that the main method will be called (invoked) with a parameter which then will cause an error because i wrote the main with no parameter in it. but i don't understand how it is possible since i am (the programmer) decide how to invoked main in my own program, so i just won't create a situation that will require to invoked main with any parameter at all. And if i may need to call main with a parameter then of curse i will write main as needed.

you actually made it more clear this times and i think i pretty much got you idea of why we need it. thanks a lot!! you have been very helpful.

p.s - the first time you explained probably was very goo, but English is not my native language so of course it makes it a lot more difficult to understand things sometimes.

thanks again!!

Markus Ylisiurunen
Markus Ylisiurunen
15,034 Points

No problem! Things like this helps me too because I have to think about it. To be honest I haven't ever thought about this before :D

English isn't my native language either. You did a great job and your English is very good :)