when I was first learning Java in college I remember one of the first things that really stumped me was static versus non-static variables and methods it just wasn't clicking for me and I felt so lost so if you feel the same way believe me it's not just you here we'll go over exactly what the static keyword means for both fields and methods with plenty of examples and you'll know exactly when to make something static or non-static but first I also have a full Java course available in the link down in the description there you'll find
over eight hours of exclusive video lessons covering dozens of java topics so if you haven't yet go check it out now so what is static versus non-static well it's often helpful to start with an example so let's do that here so here I have my cat class now this cat class has a few Fields the cat's name its age the number of lives it has remaining because we all know cats start with nine and also this method meow now if you'll notice all these fields on this method are non-static none of these have the static
keyword what that means is all of them only apply to individual cat objects and not to the cat class itself now let me show you exactly what I mean by that so to demonstrate let's go over to our main method here and let's go ahead and create a new cat object like this so cat my cat equals new pet and then of course we can say my cat dot meow we can call this meow method on our individual cat object however what we can't do is say cat the class dot meow the cat class can't
meow only individual actual cats can meow that's what non-static means they make sense and apply only to individual cat objects and not to the class itself and that kind of makes sense right remember we can think of a class as kind of a blueprint for creating objects of that class this cat class is isn't an actual cat it's just a blueprint of what a cat has and what it's able to do so a cat has a name an age a number of lives remaining and a cat can meow so we can use our cat class
to create individual cats and then those individual cats can meow but we can't just tell the cat blueprint to meow it just doesn't make sense so non-static methods can only be called on individual objects now the same goes for these fields here the name the age and the number of lives remaining these are also non-static so we can go back to our main method here and take our my cat object and set its name like this my cat dot name equals Stella and I can set Stella's age my cat.age equals eight and of course you
can create more than just one individual cat object you can create as many as you want and since the name and age fields are non-static each individual cat object object that you create can have its own separate name and age and they don't conflict with each other the cat class itself doesn't have its own name and age just each individual cat does so if I tried to just take my cat class and set its name equal to George that just doesn't make sense and it doesn't work and I get an error that tells me this
non-static field name can't be referenced from a static context it's just telling me hey this name field is non-static so it only makes sense in the context of an individual cat object not the cat class so then if that's non-static what are static methods in fields static methods and fields are not different per each individual cat object one example of a static field that we can add to our cat class is perhaps a field that holds the total number of cat objects that have been created throughout the life of our program we would want to
make that static because that's a common value that's shared among all the objects of the cat class and isn't different per each individual cat so to make that we would declare a private static int and we'll just call it cat count and we'll initialize it to zero static Fields can of course also be public or protected or whatever but in this case we want our cat count to be private so it can't just be manipulated by other classes all around the program we want to have full control of it here in this class so then
we can take our static cat count field and then every time the program creates a new cat we want to increment that now that will only happen down here in our Constructor so in here we can just say cat count plus plus so now every time a cat is created we'll increment our cat count that way we can keep track of the total number of cats we created throughout the program so now that we're keeping track of the number of cats created throughout our program how do we actually access that count from outside of this
class well that's actually where a static method is perfectly suited so we can create a standard getter method for this cat count field but we'll make the getter method static so that we can get it through the cat class itself and we don't have to get it through an individual cat object so then down here we can say public static int get cat count and then all we have to do in that method is return cat count and then back over in our main method we can call that method on the cat class because it's
static so we can say cat dot get cat count and then to make sure that's working we can go ahead and print out our cat count both before and after we create our cat object like this so if everything's working right it should print out zero here at the beginning of the program before any cats are created and then one here at the end after it's been created so let's check it out and yes looks good now it's important to note that non-static fields and methods can never be used without calling them on an individual
object and we know that because we can't just call Cat you know our class dot name now similarly though let's go back over to our cat class inside this static get cat count method that we created now inside this static method we are not allowed to refer to any non-static Fields so inside this static method I couldn't print out this cat's age you'll see here I get an error that a non-static field can't be referenced from a static context and the reason is that it just doesn't make sense non-static Fields only make sense in the
context of an individual object and they can be different for each individual object so inside a static method when you're kind of operating at the class level instead of the individual object object level it just doesn't make any sense to refer to any non-static field so it's not allowed however the opposite is not the case technically you can access static fields and methods through an individual object so if we go back to our main method here now technically I am allowed to call my cat dot get cat count as you can see I'm not getting
any errors here so here I'm calling a static method from an individual object however this is discouraged and you're going to get a warning like I'm getting here it's saying this static method get cat count is being accessed via an instance and it's encouraging you to change it to use the cat class to access it instead of the individual object and so if I want to do that which I should I can just click this suggestion here and it'll fix it for me so you should always access static methods and Fields through the class and
not through any individual object so you might ask why though if it works through an individual object why not just use one what does it hurt well the reason is that it can be misleading and confusing if someone sees my cat.getcat count they might think they're getting some kind of account that only applies to this individual cat when in fact it applies to the class as a whole this is even more important when you're modifying something so perhaps you had a static method that modified something on that class if somebody sees that being called with
an individual object they might think oh I'm just changing this value for this individual object when in fact they're changing it for the whole class so that's why it's important to always access these static fields and methods through a static context through the class and not through any individual object another great example where you'll see static Fields used all the time is for constants constants are variables that don't ever change they stay the same throughout the entire run time of your program so for our cat class here one value that we might want to have
in our program that would be the same for every cat would be a maximum number of lives a cat has which is of course nine so up here we can have a public static final we use the final keyword to make it a constant so it can't be changed later if you want to learn more about final I have a whole video on Final here it'll be an INT and we'll call it Max lives and of course we'll set it equal to nine so this is just saying that for any cat no matter how many
lives it might have remaining here at any given time the maximum number of lives for any cat is nine and you can retrieve that value for whatever you need both inside the class here or outside the class just by accessing it through the class name because it's public and it's static so for example maybe it may makes sense that when every cat is created we start its number of lives remaining with the number of Max lives that would make sense so down here in our Constructor while we're creating a new cat we can set its
lives remaining equal to Max lives and if for some reason we wanted to access this Max lives field from outside the cat class so back over here in our main class because that field is static we can access it directly on the cat class itself so we can say cat dot Max lives and we can print that out if we like just to prove that it works go ahead and run it and yes we get our maximum lives of nine so the overall question how do you determine whether you should make any given field or
method static or non-static well if the method or field that you're creating only makes sense for an individual object an individual instance of that class or if it has to be different forever every object of that class then it will have to be non-static but if it's something more at the class level or something that should be the same or a shared value among all of your class then it probably makes more sense to be static like I said I totally understand that this can be really confusing at first it absolutely was for me but
I promise you after a while it will sink in and this holds static versus non-static thing will be something you hardly even have to think about and as always if you enjoyed this video or learned something please let me know by leaving a like and if you didn't go ahead and leave a dislike and Shout at me in the comments thank you so much for watching and being here with me I will see you next time