In this video we're going to look at the static keyword and this time for real, because we have seen
the static keyword a bunch of times before.
And we used it as well to a degree, but we never actually actively used it with understanding it.
And it didn't make sense before because we needed to understand what methods are so member methods or
class methods in order to understand what static is.
Because static is a method that doesn't need an object on which it can be called.
So what does that mean?
Well, let's say we have a customer here and we want to use customer dot any given method that we have
about that customer okay.
So for example set details.
Right.
And if I want to use this set details method I need to give it a name.
So let's say the customer is Dennis and the address is Main Street.
Then I can use it right.
So this is a non static method.
And this method outside of its own class.
It can only be called on an object of its class.
So set details can only be called on an object of type customer okay.
And it can be called inside of its class itself.
So you could go ahead and call set details in here.
That would work where you just assign the name and the address in here okay.
So that would work.
But that's because the method is in the same class right now.
If you want to call it from the outside, you have to do it on a customer object.
Now what if I want to have a method that I can call even if I don't create an object of my customer
class?
Well, therefore, I can use the static keyword so I can use public static void.
Do some customer stuff for example.
And what this method does do is going to do is it's just going to say something like I'm doing some
customer stuff.
That's it.
Right?
So it's not really important what it says.
It's just about the idea of what this static void main method can do now.
So the static keyword indicates that we don't need to call it on an object.
We can call it without creating an object.
So what does that mean.
Now I can go ahead and use customer dot do some customer stuff.
So I didn't have to create an by the way, in order to really demonstrate it, I'm going to get rid
of this.
I didn't need to create an object of customer in order to call this method.
Now I could just call it and now I can call it on the customer class itself, not on a customer object.
And why do I call it on the customer class?
Well, that's because that's where this method is.
So this customer is just saying, okay, this method is inside of do some customer stuff.
So if it were somewhere else like in here for example.
So static void my method, I could call it as well here.
My method mother.
Uh my method my mother.
So there we are.
So now this my method can also be called in here, my method.
And it can be called because it is static.
If it weren't static, I couldn't call it in the main method because the method main is static.
And the only way how I could otherwise use my method is by creating a program object.
So program program new program.
And then I could say program dot my method.
So that would work for example okay.
But if I make it static then this won't work suddenly because I cannot call it on an object, I have
to call it by itself, which is what we do here as well.
So we call the method independently.
And therefore what you also get is that this method never knows anything about a customer object.
So that's something you have to be aware of.
So let's say I had a customer here.
So customer customer my customer new customer.
And now I want to define or call to do some customer stuff.
You see I cannot call this method here because it said I want to be independent.
Well guess what.
Now you're independent, but you cannot work on my objects anymore.
I can't accept that.
So that's why this here doesn't work.
Because the do some customer stuff method is static and cannot be called on objects, which has its
advantages and disadvantages.
So do you see another really, really powerful method that we are using all the time that is not used
with an object?
Well, I can see two that we're using all the time.
That's the read key.
And the right line.
Now guess what?
Those two methods are static.
So if you hover over read key and you press the control key and you click into it, you will see that
it says public static console key info read key.
So it returns something of type console key info, whatever that is.
Right.
But it is a static method.
And now the same goes for our right line because you see it uses the console class name because console
is a class dot write line.
So if we hover over that, you can see or click into it with the control key held.
We can see it's also a static method.
So the right line method is a static method.
So now you have seen where everything comes together right.
So you have the static keyword which indicates that a.
So to really bring home the point or like simplify this, let's put it into one statement okay.
So in C sharp the static keyword is used to declare members of a class that belong to the class itself,
rather than to any specific instance of the class.
Let that sink in.
So here you see it belongs to the customer class, but it does not belong to an object of the class.
So it does not belong to a specific instance which is an object.
It is an object is a specific instance of a class.
It does not belong to my customer, so I cannot call it onto my customer.
Good.
Okay, so that was the static keyword for you, but only with methods.
So we also need to look at the static keyword with variables.
But the static keyword can also be used for classes, constructors, properties and fields.
And as we've seen with methods.
So let's look at fields in the next video.