Static Method In Java Tutorial #76

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome back to another Java tutorial in this video I'm going to show you all about the static method in Java how it's used and how it's different than a regular method but first if you're new here my name is Alex on this channel I post a Java tutorial just like this one every single week also if you want a job in Tech there's a coding bootcamp I highly recommend along with 1 000 off all that information is in the link down in the description so let's start learning about static methods in Java by starting a new project together we'll go to new Java project we'll call this something like my project hit finish and then in the source folder go to new class we'll call this something like main hit this public static void Main checkbox and then hit finish a static method in Java means it belongs to the class itself rather than an instance of a class and I know that sounds really confusing I wouldn't have known what that meant even in college so let's just write it down instance of class versus class itself so let's make a method that is static and one that is not static this one will be static it won't return anything and we'll just call it say hi this is going to print out hi and then we'll have another one here that is not static and this one will be something like about say pancake and this one will say pancake if we want to call say hi we can do that super easily by just typing say hi and if we run this we'll see high gets printed out if we want to call say pancake say pancake we get a red underline we can't call it this way so let's remind ourselves again with a little better comment here so static method means the method belongs to the class itself and then a non-static method means it belongs to the instance of a class this first one is static so we'll put that with the right labels an instance of a class means you have to do this since our class name is main we have to Define it main we'll call it m equals new main this creates a main object from the main class so m is the object and Main is the Class M is the instance of this main class so if we do m dot the error goes away if we save and run this we'll see Hi and then pancake because when we click the Green Run button we run code in the main methods curly braces the first line is just a comment the next line is calling the say hi method and we can do that since it's static it belongs to the class so it has access to it so we can call it and run the code inside of here which is why we get high next we see another comment and we create an instance of the main class called M since non-static methods belong to the instance of a class we can call it like this and it works so we run the code inside of say pancake and we see pancake I know it can be pretty confusing but the big difference is how you call it you can call it directly or you have to call it through making this first and then doing that dot the method let's do another simple example here we'll remove both of these let's say that method's in another class so if we go to new class we'll call it something like other class and um I actually shouldn't have deleted those I should just copy them over so I'm just going to copy them and put that in here to access the static method versus the regular method is the exact same way so if we want to call say hi from the main class we would do say hi except now since it's in a different class rather than the same class we have to say which class it belongs to so other class dot say hi this is the class itself a static method belongs to the class itself whereas a non-static method belongs to the instance of a class so we have to call that something like oh we created an instance of a class called o here so we have to do O DOT say pancake save and run and we get the result as expected here you could also do o dot say hi we see say hi here so if we call it like that you get this yellow underline it will still work but it's saying that this static method see stay say hi has the static keyword since it's static it really should be called like this instead of like this so you would just change that to a class name and then the method so I'm just going to leave it there that's really as simple as it is the word static non-static class instance and object are all like pretty confusing when jumbled together like that but the key takeaway I want you to get from this is that static you call like this or if it's in the same class you don't need that and non-static methods you call like this you have to define the instance of a class first and then call it through that dot after the variable name so I hope this helped you and I'll see you in the next video
Info
Channel: Alex Lee
Views: 30,376
Rating: undefined out of 5
Keywords: static method in java, static method java, static method java example, java static method, java static, static methods, static and non static method in java, static method, non-static method, non-static java methods, what is non static method?, java non static method, java methods tutorial, static and nonstatic methods, static in java, static java meaning, non static method in java, static method vs non static method java, static method example, Static method example in java
Id: xKZ3sMimQYM
Channel Id: undefined
Length: 5min 53sec (353 seconds)
Published: Thu Jun 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.