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

Can anyone explain this line to me please: public static void main (String [] arg) {}

Is there any video of him explaining how to start from the scratch as well as what does all that mean?

2 Answers

He explains it later in the course but i will give you a quick summary of what that means so you can relax your curious itchy mind :)

alright lets break this down:

  • public static void main (String [] args) {}: this whole thing is the main method of your program, this method is what the launcher looks for in your entire project and where your program is launched from, basically your starting point of execution.

  • public: this is the accessibility declarator marking from where in your project you have access to this method. read this for more on access control https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

  • static: this keyword marks your method as being accessible even without creating an instance of the class. Maybe you're familiar with how normally, when you wanna use a method on an object from a class outside of the one you are coding in, you have to create an instance of that object and call the method on the object like so: Scanner myScanner = new Scanner(System.in); and then you can call methods on the object to execute them like so: myScanner.nextLine(); Well, there are methods on classes which you can access without creating an instance of those and instead just call the method on the class itself. The java.lang.Math class is a perfect example of this. It contains nothing but those so called static methods so you can just say for example Math.abs(-8) and that will return the absolute value of -8 which is 8. You see, here you're calling the method .abs() directly on the class Math without creating an instance of said class by calling a constructor like you normally would. This works only if the method is marked with the keyword 'static'.

  • void: this keyword marks the method's return type and void means that it doesn't return anything. So, for example that last method I was talking about on the Math class called .abs() does not have return type void it actually has the return type 'int' (which is what you get out of it in case you give it an 'int' as its parameter, you can give it something else aswell which will call a different method with the same name but with a different return type, accepted parameters for .abs() are int, float, double and long, this isn't that important though). You can make a method return any object you wan't even objects that are instances of classes you created yourself.

  • main(): this is just the name of the method the launcher looks for to set the starting point from where to run your program.

  • (String [] args) read-> String array (called) arguments: this is the parameter the method takes, you can think of this as a sort of initial setting your program starts with, so for example if you know how to run a program from a command prompt you could call the main method with an array of strings like so: main("strawberry", "banana", "cream cheese") and then you could set what to do with those values but normally your program starts without any arguments, which is fine. Wouldn't worry too much about this one, you're not gonna get confronted with it for quite a while.

hope this sets your mind to ease

stay curious, keep digging deep, make sure you understand and don't just learn to repeat what you're told :)

happy coding and lot's of success

Matthias

Wow. Thank you so much. I just bought the book "Beginning programming with Java for Dummies" and although I prefer learning practically over here through exercise there are amazing definitions and explanations over there on how Java works so I'll use that too. :)

The courses are laid out such that they start by teaching simple concepts and then move on to more complex topics as the courses goes on. The code you quote contains quite a few complex things that is not going to be easy to understand without you having some familiarity with java already. It is only included in these beginner courses because it has to be there in order for the program to run at all.

Treehouse does end up explaining what is going on in that line over the course of the various Java courses. So you don't have to worry about not understanding it by the end of your Java education at Treehouse, but you shouldn't rush things.

For now you should just ignore those lines and wait until learn more about classes, methods and other things like that. Trying to understand that line right now will be more trouble that it is worth as it touches upon quite a few complex subject you have not learned yet.

Thank you very much. I understand all this instructions besides that one, so that was bothering me... :D I just wanted to make sure that he will explain this later, once I build up my knowledge. Thanks for the answer !