Java Main Method Tutorial - Everything You Need to Know

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody today we're gonna be learning about the main method together haven't made a video on this yet first my name is Alex I make a Java tutorial on this channel every single week so if you're new here and you might be interested in seeing that then please consider subscribing let's start learning about the main method by making a new Java project call it our main project hit finish and then inside of the source folder go to new class I will just call it main and then click this public static void main checkbox and let's get started so we hear this all the time the main method the main men when you run code you run code inside the main method that's how Java intended things to be if we run this we see nothing prints out because nothing is in the main method besides this comment which doesn't do anything a method has this structure and runs code inside of its curly braces so to make a regular method it would be something like this say hi with things in the curly braces it's complaining because we need to have a return type if we don't want to return anything we're just going to type void here and all the method does is run code inside of the curly braces in this case this method is just going to say hi if we run this we don't see anything happen because there's still nothing in the main method so the main method lets us control the code we want to run so if we wanted to use this a high method when we click run we just have to call it inside the main method boom it's arguing that we need to make like this main object and then use it that way but we can get around that by typing static here save and run we see hi because when we click run we run code in the curly braces we see a method called say hi this class knows a method say hi because it's right here and we've run code inside of these curly braces in print hi which is why we have hi here this is really basic stuff but it's really important to just nail this if you take a step back you start to realize well these keywords here static void are the same keywords up here static void so now this whole line of what looks like nonsense is kind of starting to make sense it's just like any other method we could also have public here which just means that if you make a main object from this class and do a dot then this method would pop up not main because the main method is special now let's go on to these parentheses well why is there this string square brackets args in here what is that supposed to do if we have arguments in a regular method like this we could say say something and then pass in a string s and instead of printing hi we can print s and say like hey this is no longer called say hi so you have to change it say we run this it'll prints hey so we know how a regular method uses parameters that we pass in the code you call the method you pass in this parameter which runs code inside of here since this is the parameter this is now s so we print out S which is hey so how does that apply to this string square brackets arcs well this looks like an array string square brackets is an array of strings so let's see how would we how we would use an array of strings here in a regular method we can make a string array like this string array s equals and then throw some strings in here hey sup and then pass that as the parameter so now this s which is a string array matches this s which is also a string array so if we run this it spits out gibberish because that's what we expect a string array will spit out gibberish if you just print it so we can just get like one element of it and it works so how do we use this arcs well args is the arguments when you run the Java program so let's destroy all this and get that in your head right now let's print out that variable arcs there's no red underline because ARDS is the variable in the parameter so this method knows what args is let's run it and you see some gibberish which is the memory how do you set the arguments well you can do it from the command line or from your editor we're in Eclipse so to do it in here we would go to run configurations and we click our main class which is the one that we're using arguments tab and then pass in the arguments here let's say we have hey sup just like before just separated by spaces apply run and you see more gibberish because again this is an array of strings so we need to get an element from it say run and now we get sup because when we run this it passes in these two string parameters well this one array of strings it passes it into the main method and runs code with that variable just like we had before we can do the first one with this and we get both so args is short for arguments they can help you change the configuration of the code based on what you pass in so if I do run with the parameter for maybe that'll change what happens when we run it for example if args of 0 equals oops if our existing 0 since the string attitude equals hey then do this bah-bah-bah otherwise if it's equal to high we can do something different and I think you see where all the configuration can come in it's kind of like a little code that changes the running to your liking we can also do this from the command line for example so I'm just gonna make a new Java file boom I'm just gonna save this on my desktop desktop called main Java so now we've got this little Java file on the desktop I'm going to open terminal and then we're going to navigate to the Java file our current directory is 1 we are not on the desktop so we'll change directory CD to the desktop and we see main dot Java we first have to compile it so we do Java C main Java this turns it into machine code and now we can just do Java main boom and that runs it except we get an error because we're trying to use the elements in that string array args but we haven't passed in the arguments here so to do it in command line you pass it in just right here on the command line hey sup enter hey sup these are the arguments we passed in which stores into the args string array and then we print out those two so all it is is all this the string arguments array is it's just a parameter that you can use inside the method just like parameters inside of any other method this confuse me for a long time and I didn't know how to use it until like the third year of college yeah that's basically into the main method it just runs the code inside the main method when you hit run you can configure it using the orgs and that's really all there is to it public it can be seen static you don't have to make an object to call the main method void it doesn't return anything in main is the name we give it so that Java knows that it's the main method so I hope this helped you out a little let me know if you learned something down below and I'll see you in the next video have a great day [Music]
Info
Channel: Alex Lee
Views: 60,567
Rating: 4.9427547 out of 5
Keywords: java main method, main method, main method java, main method in java, java main method tutorial, main method java tutorial, java main, main java, main java class, main, alex lee, java
Id: xJ2rCeDtyyk
Channel Id: undefined
Length: 9min 50sec (590 seconds)
Published: Thu Mar 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.