Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

Grigorij Schleifer
Grigorij Schleifer
10,363 Points

Array[] args in wrapping up .... xFiles again

Hello community,

I habe problems to understand what it means that the User should be setup using values from the args array

User author = new User(args[0], args[1]);

In the User class we have two Strings for firstName and lastName, but there is no array in the User class at all :(

Does it means that every class i create stores every different variable in the Array[] args???? OMG .... it sound crazy ....

Sorry for my bad english, if i am better in java i will take courses in english :)

2 Answers

Allan Clark
Allan Clark
10,810 Points

The args array stores command line arguments from the user at the start of the code. It is a part of the main method signature, and generally only exists inside that method. To create a new User as you described it would look something like this:

public static void main(String[] args) {

       User author = new User(args[0], args[1]);

}

*Note: Since I don't know how the User class is set up, for this example lets say the User class accepts first name and last name when being created.

Suppose the program was started like this in the command line:

java MyProgram Bob Saget

//remember this is in the command line not a part of your code

This will start the program and the args array will store ["Bob", "Saget"]. Then inside the main method a User is created with that first and last name.

Heres a Stacked Overflow response about this as well.

http://stackoverflow.com/questions/890966/what-is-string-args-parameter-in-main-method-java