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

Grigorij Schleifer
Grigorij Schleifer
10,365 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