Java Main Method Explained - What Does All That Stuff Mean?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when you got started programming in java i bet somebody just told you that you had to have this public static void main string args and in there is where you write your code then i bet you probably thought oh wait what does that mean wait wait what's with public static void main string what does all that mean and they probably told you shut up nope don't ask no shut up stop no just program just do it write it put that in there that's what you got to do and you probably went whoa okay all right i'll do it but that question is probably still going on your head what does all of that actually mean so i promise that in just a few minutes you're gonna know what each individual piece of that means what it does and what it's used for so let's get to it so i think the best way to think about what is happening here is that the jre the java runtime environment on your computer has to call your program's main method to trigger the program to run so for example i have a class here called main method explanation that has a main method and what the jre is actually going to do is call this main method uh something like this so with the main method explanation dot main and it's going to pass in arguments and all the pieces of this public static void main string args uh method signature are designed to make that call work so first things first public why is it public well it's public because this method has to be callable by something outside of this class if this method was something else like private or protected the jre wouldn't have the access to call it so the method has to be public this was private instead only this class would be able to call it and so it's it's kind of like saying hey i'm a great program and the jre goes great how can i run you he's like yeah he can't he can't only i can run me and that's pretty useless program so next why static so a static method is a method that can be called on a class without needing an instance of that class to run it against and that's how the jre is actually going to call your program it's not going to create an object of main method explanation and then run a main method on it it's just going to run the main method directly on the class exactly as i have it written here so here's what it's not going to do create a new variable of type main method explanation called explanation equals new main method explanation and then take explanation dot main it's not going to do that it's designed to make the call directly on your class like this so that's why it has to be static it's not going to be called on an instance of your class it's going to be called on your class so your main method has to be static now next is this void now void is actually the return type of your main method just like you have methods that return an int or a long or a string or whatever your main method just returns nothing it's void it has no need to actually return anything so when your program gets called it gets run and it finishes it doesn't have to return anything when this main method is done your program just ends now next why is it called main it just is it's just called main you can't call it anything else the jre is going to call your class dot main and so your main method better be called main or won't work so i know at the beginning i told you yes people told you and it's just the way it is in this case that's just the way it is that's what the jbm is going to call that's what you better name your method now the last part of this is probably the most interesting this string array called args and what this actually is is an array of strings that are arguments that can be passed to your program and if you've never used it it can actually be pretty cool now a lot of programs that you write you just don't care what is passed in in this uh array of strings arguments you don't need to do anything your program is going to do what it's going to do you don't need to give it any other argument at the beginning you might take input later or something like that but you don't need to when you kick off the program give it input you get input from the user later or something but you can do it so i'm going to show you a quick example of how you can actually take in an argument and use it in your program so let's say you just wanted to print a message from whoever's starting up your program and they can answer that message as the argument you just want to print it out so you can do system dot out dot print line args zero so that's just getting the zeroth the first element on this array and it's going to print it out so how do you pass in that argument now i'm going to show you a couple ways first is in the ide i'm using i'm using eclipse right here so if you're programming eclipse for example if you want to change these arguments you can right click anywhere on the class and go to run as and then click run configurations and then you'll see this arguments tab and that's where you can put in your arguments so for example we can put in chimichanga and apply and then we can hit run and what that's going to do is pass in as an array one element array um with the string chimichanga and that's going to be at the zeroth position of those args as you can see it prints out chimichanga so you can imagine what kind of things you can do with this you could let's say you want to print out something like x number of times you can give it that number in the input arguments there's all kinds of fancy stuff you could do with that but also if you're not running in an ide and you're just using a text editor or something you can still do this so here's my folder with main method explanation.java in it and i can open a powershell window here you can use command window or whatever you've got this is just basically a command window and so i can compile my program with java c and what's the name of it java c main method explanation dot java so it's compiled you can see my class file here and now i can run that command with java space main method explanation and then i can just put my arguments right after that so i can also put jimmy junga's and i can see it printed out chimichangas but something to note if you are actually using these arguments in your program you have to pass them in so for example here if you don't pass it in you're going to have problems so we could run that last command again just without that parameter and you could see oh no i got an array index out of bounds exception it was looking for something in an array that wasn't there and blew up so you can run into problems if you don't use that right but in the right hands it can offer some pretty cool functionality so i hope you learned something today hope i've demystified a little bit of the java world here today for you and if i did give me a thumbs up it's super appreciated if you'd like to see more videos like this in the future hit that subscription button thanks so much everybody see you next time
Info
Channel: Coding with John
Views: 131,644
Rating: undefined out of 5
Keywords: java, programming tutorial, learn java, java programming, java main method explained, public static void main string args, public static void, main string args, java public static void main string args, java main method, java main, java main class, java tutorial, main method, alex lee, alex lee main method, alex lee main, java static, java static methods, java static method, static methods in java, java basics, public static void main in java, java public static void main
Id: P-_Nzi_mCRo
Channel Id: undefined
Length: 7min 10sec (430 seconds)
Published: Tue Nov 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.