Hey, everyone.
Today we’ll be talking about static versus
non-static.
One thing you should know is that all things
you create are non-static by default.
Unless you add the reserved keyword static,
they won’t be static.
They will be non-static.
So what can you make static?
There are a bunch of things including classes,
methods, properties.
In summary, static means that whatever is
marked as that, it just basically does not
belong to any object instance created.
It belongs to the class itself.
You remember how the class is a template?
The object is created from that template.
So it’s instantiated from that template.
That is the class.
When we make something static, then it belongs
to the class.
It belongs to the template.
So whenever you’re instantiating the class
and you get your object, you cannot use that
static member from the object instance that
you have created.
One good example is Console.WriteLine().
We’ve been using that forever now.
Console is a class, a custom type.
And WriteLine() is a static method.
Let’s create a project.
We call it StaticNonStatic.
One thing to notice is that this whole time
our Main method, where the execution of the
program begins, is a static method.
And you can see that void is the return type,
so it’s not returning anything.
But that is irrelevant.
What’s relevant to this example is that
we have seen static this whole time, and now
we know what it stands for.
So that means that program does not need to
be instantiated.
It can just use Main from the class itself.
Let’s create a class named Person.
Then let’s create two properties called
Firstname and Lastname.
Now let’s create a constructor.
We know about all these things.
If you missed the previous series, please
go and watch them because that is important
to understand this example now.
We will pass in Firstname and Lastname as
parameters.
class Person
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public Person(string argFirstname, string
argLastname)
{
Firstname = argFirstname;
Lastname = argLastname;
}
}
What we’re doing here is we’re creating
our constructor and we’re initializing the
property values.
And now let’s instantiate this class in
Main.
Person person = new Person(“Avetis”, “Ghukasyan”);
I create a new person object and I pass in
my first name and last name.
In the class Person, all that you see,
class Person
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public Person(string argFirstname, string
argLastname)
{
Firstname = argFirstname;
Lastname = argLastname;
}
}
is all non-static.
So when we have any object instance created,
we can access these properties.
And obviously, we can access the constructor.
Now let’s create a static method.
class Person
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public Person(string argFirstname, string
argLastname)
{
Firstname = argFirstname;
Lastname = argLastname;
}
public static string FormatFullname(string
argFirstname, string argLastname)
{
return $"{argFirstname} - {argLastname}";
}
}
Now we 
have our static method.
Let’s try to use this in Main.
person is the object instance.
When you use the dot notation, you have no
access to the static method FormatFullname().
However, if you type in Person, which is the
class itself and use dot notation, you will
have access to the static method FormatFullname().
Person person = new Person(“Avetis”, “Ghukasyan”);
Console.WriteLine(Person.FormatFullname(“Avetis”,
“Ghukasyan”));
And we can use the other static method of
Console.Writeline() that we’ve been using
in order to print out to the console the formatted
full name.
And let’s see what happens.
The output is:
Avetis – Ghukasyan
We get the formatted full name.
There are a few things to understand.
The first thing is we already saw that the
static method FormatFullname() belongs to
the class itself.
So the object instance cannot use it.
Another thing to understand is that we cannot
use object instance members.
So we cannot use the Firstname and Lastname
properties of the class Person inside of our
static method.
Let’s try to do this.
I hover over it and it says, “Cannot access
non-static property ‘Firstname’ in static
context.”
And that’s exactly what I just said.
That’s one thing to know.
However, the opposite is true.
So if we use FormatFullname() in our non-static
constructor, we will be able to do that.
So let’s do that.
class Person
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public Person(string argFirstname, string
argLastname)
{
Firstname = argFirstname;
Lastname = argLastname;
FormatFullname(Firstname, Lastname);
}
public static string FormatFullname(string
argFirstname, string argLastname)
{
return $"{argFirstname} - {argLastname}";
}
}
This begs a question.
When do you use static and when do you use
non-static?
And that’s a very good question.
But the answer is not so black and white.
But I guess the general definition is you
want to use statics when you just want the
method for some general-purpose task.
It does not need any of the instance members.
So in this case, it does not need in this
case the Firstname and the Lastname and anything
else that is non-static and is a member of
that class.
It’s a general-purpose thing in and of itself.
But in this case, it would’ve been much
better if we made the FormatFullname() method
non-static.
This would make a lot more sense because then
we wouldn’t have to pass the Firstname and
the Lastname as parameters argFirstname and
argLastname to the method FormatFullname().
We could just use the Firstname and the Lastname
from the object instance itself since we’re
creating it and we have these from the very
beginning.
class Person
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public Person(string argFirstname, string
argLastname)
{
Firstname = argFirstname;
Lastname = argLastname;
}
public static string FormatFullname()
{
return $"{Firstname} - {Lastname}";
}
}
So it makes sense to have this as non-static.
However, in some cases where you are trying
to retrieve things such as the application
settings or you’re doing some calculations
or conversions or some other general-purpose
tasks that do not require the object instance
members, you can make that member static.
That is pretty much the rule of thumb.
However, it is a bit of an artistic choice.
So you have to learn this through experience.
However, know that that’s what statics are
and that’s when you should use them.
Use that rule of thumb.
A few other things to know are that if a class
is static, then all of its members also have
to be static.
I can make a class static by using the keyword
static.
static class Person
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public Person(string argFirstname, string
argLastname)
{
Firstname = argFirstname;
Lastname = argLastname;
}
public static string FormatFullname()
{
return $"{Firstname} - {Lastname}";
}
}
Another thing is that you cannot inherit a
static class.
We’ll talk about inheritance later on, but
just know that whenever a class is static,
you cannot inherit it.
Another one is that there is only one copy
of static in memory.
That’s also another good thing to know and
keep in mind as you’re going through your
programming journey.
You might not understand these things for
now, but just keep that in mind.
And once the opportunity presents itself and
you have to use this knowledge, you will then
internalize it.
So keep that in mind, please.
Another thing is that your static class cannot
be instantiated.
Knowing all these things, keeping them in
mind, these are a bit of an academic knowledge
since it is a little tough to show examples
that will apply to all of the other scenarios
that you will experience.
But knowing these principles and knowing the
rule of thumb of when to use them and when
not to use them will allow you to internalize
the knowledge when the problem does present
itself.
The last thing to know about it is there’s
a tendency to take static members out of the
class and dump them in another class that
is meant for static methods.
And they make that a helper class.
And if you look into that class, you will
see all of these unrelated methods.
The reason why this is bad is that it’s
very difficult to track and maintain as the
class grows bigger and bigger.
And it’s just a very messy way of going
about it.
If you really need to do that, at least categorize
them by the action that the methods or other
type of static members are trying to take
and categorize them like that.
Once again, statics, non-statics, it’s a
bit of an artistic choice.
Keep these tips in mind.
And always question whether what you need
to make static really needs to be made static.
For example, the reason why I made this example
is honestly to show you that sometimes you
just need to question it and change it back
to non-static because it does not make sense
for it to be static.
In this case, as you saw, it did not make
sense for the FormatFullname() to be static
because it was much cleaner to have it as
non-static.
And obviously, this method needed the object
instance members, which were the Firstname
and the Lastname.
That was a lot of information.
I hope I didn’t confuse you a lot.
Please keep this in mind.
Re-watch this video.
Read more about statics and non-statics.
And as I said, once the problem presents itself,
you will internalize this knowledge a lot
deeper, and you will be able to use them more
effectively next time around.
If you like this video, please give it a thumbs
up.
And if you haven’t subscribed, please do
so.
And if you have any questions, please leave
them below.
Thank you for watching.
