Java static keyword ⚑

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey how's it going everybody it's your bro hope you're doing well and in this video i'm going to teach you guys how the static keyword modifier works in java so sit back relax and enjoy the show if you find this video helpful please remember to like comment and subscribe your support will help keep this channel running all right guys and gals i'm going to be explaining the static keyword modifier this is a keyword modifier that can be applied to a variable a method or even classes but that's a separate video so anything that is static is also known as a static member and a good way of thinking about this is that the class that contains that static member now owns that member whether it's a variable or a method so anything that the class owns is shared by all instances of that class meaning if we create objects from this class they all have to share this one variable or method there is only one copy so let me explain this with an example i have a class called friend and we're going to be constructing some friend objects so i have the constructor and one parameter set up where we have to send in a name so let's create a static variable called number of friends so all instances of the friend class have to share this one variable so we use the static keyword modifier then the data type of the variable and let's say this is an integer and this will be called number of friends and if we were to display this system.out.printline now this is owned by the class itself so we do not need to create a friend object we can just type in the name of the class friend followed by what we're trying to access number of friends and i'm using eclipse you can see here that i have this little red s next to this variable that shows that this is a static variable and if we were to print the number of friends that we have it's unfortunately zero so let's create some friends so let's create one friend object so friend let's call him friend one for the instance name equals new friend and assign a friend name of baby spongebob because that's the first thing i thought of and then within the constructor let's increment the amount of friends that we have so after everything is done we'll add one to the number of friends that we have so we now have one friend now let's create another friend the name of this instance will be friend two and we'll pass in a name of patrick and now the number of friends that we have is two so both friend 1 and friend 2 are sharing the same number of friends variable because there is only a single copy and the class itself owns the static member and if we were to create another friend guess what's gonna happen friend three the name this time will be squidward and we now have three friends now it's entirely possible to access a static variable using an object instance name itself so let's replace friend with friend one so it is possible to access this static variable using the named instance of an object from this class but it's not recommended and you can see here that there's a warning the static field friend.number of friends should be accessed in a static way so it is best practice to type in the name of the class that owns the static member followed by the static member that you're trying to access so this will display the number of friends that we have and since there is only one copy all of these instances of the friend class are forced to share this static member so what do you think would happen if we removed the static modifier from this variable well for one we can no longer access this variable in a static way so you can see that it says cannot make a static reference to the non-static field friend dot number of friends so each of these friend objects now has their very own copy of the number of friends variable and in order to display one of these copies of the number of friends variable we would type in the name of the instance of this class for example friend one and their own number of friends is one same thing goes for friend two as well as friend three so if we change this back to static we can make a static reference to this static variable and this will display the total amount of friends that we have created from this class which is three and not only can you apply the static keyword modifier to a variable but you can also apply it to a method as well so let's create a static method that returns the amount of friends that we have within a message so let's use the static keyword modifier and then the return type is void and the name will be display friends and within this method we'll just have a print line statement so system.out.printline you have plus number of friends plus friends within a string so we no longer need this line now the preferred way of calling this method is by the class name and not the name of one of its instances so if we want to call this method this static method of the french class we're going to type the name of the french class and not one of these instances dot and then the method that we want to call and you can see that we have a static method called display friends and when we call this method it's going to display our message you have three friends and then if we created another friend for example like sandy and this will be friend four and we call this method we now have four friends now a good example of a static method is the round method of the math class so we type in the name of the math class this is an unrelated example by the way dot round and you can see that this is a static method and let's take a look at the math class so let's look for round okay here is one copy of this method and you can see that it's a static method so we type in the name of the math class like it wouldn't make sense to create a map object like math math equals new math right it's much more simple just to type the name of the class and then use the function that you want dot round for example so that's the benefit of static modifiers it creates a single copy of a member a variable a method or even an inner class 2 and all instances of that class have to share that one static member and if you need to access that static member you use the name of the class itself so that's the basics of the static keyword modifier if you would like a copy of all this code i will post all of this in the comments down below but yeah that's how to use the static modifier in java hey you yeah i'm talking to you if you learn something new then you can help me help you in three easy steps by smashing that like button drop a comment down below and subscribe if you'd like to become a fellow bro [Music] you
Info
Channel: Bro Code
Views: 8,551
Rating: 5 out of 5
Keywords: Java static keyword, Java static modifier, Java, static, modifier, keyword, tutorial, java, block, variable, method, works, class loader, heap, memory, object, example
Id: wa1HzkMqY9A
Channel Id: undefined
Length: 8min 18sec (498 seconds)
Published: Tue Sep 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.