Hello everyone.
So in this session we will learn about strategic properties have a glass let us take an example.
So I have this glass student.
So this student class has three public properties.
Rule number eight and total student.
So told to go inside the total number of students present in the class.
OK.
So whenever we create a new student what will happen the value of the industrial dance will be incremented
by when initially we can assume that the value of total strawberry inside zero.
Okay.
So when we create an object
to indicate an object student S1 what will happen all the three properties will be copied.
So rule number age and told us twins
now add this line default constructor in both to default constructor will be called.
So rule number will contain garbage value.
Age will contain garbage value and total students will contain garbage value.
Similarly if I create and I the student student s do so again all these three properties will be copied.
So student as to the number three surrenders to each and student test to total students
so add this line in bold default constructively recorded serial number will contain garbage value.
Age will contain garbage value and that older students will contain garbage will you see what's happening
here is each student should average on the lumber each student should have its own age each student
should have its own total student.
Now this is wrong.
This is a very weird statement.
Okay so each student will have its own no no.
This is correct.
Each student will have it one each.
This is also correct but each student will have its total student.
Now this is kind of weird.
It's a very weird statement.
So the property told us to rent the property told us to rent this property is the combined property
of all the students and it is not the property of a single student.
Okay so the value of the student should be same for all the objects.
It will never happen that forced to rent.
That's when the value of the through interest then for us to total students are 20 and for a state or
through a third party.
It will never happen.
So the value of this property the value of the total Australian property will be same as a number of
students created.
So this property does not belong to object.
This property belongs to a class.
Okay so the report the value of total US students at one place inside the class and whoever wants to
access the to the student it will go and ask the class.
Okay so the properties which belongs to a class will declare them static using the static guild.
Okay so the properties which belongs to a class will declare them static using this tactic given before
the integer.
We will write static keyword before the integer.
Okay so what are static properties so static properties are those properties which belongs to a class
so these properties belongs to a class and not a single object and not a single student.
Okay so what will happen to the static properties separate copies not created for each object.
Okay so we can access a static property only using classes.
So what we will do we will write.
Static int total student.
We will update this line and we will use the keyword.
Static int to the students.
Now what will happen if you will create an object.
Student S1 so whenever we create an object only the non static properties get copied so roll number
and age.
They both are non-strategic properties.
So by default the properties are non static.
So whenever we create a new object only the non-strategic properties will be copied so roll number and
each these two properties will get copied.
Total students will not be copied and it will contain garbage value because their default construct
can we be called.
Similarly if you will create an ideal object let's call it student S2.
So only the non static properties will be copied.
Static properties are not copied static properties belong to class.
So at this line default constructor will be called and rule number and H contains the garbage will.
Okay so one thing to remember is only the copy of non-strategic property view only copy non-strategic
properties
Okay so separate copy is not created for each object.
When we declare them static.
Okay so student loans students will not be copied.
So what will happen to the students.
So we have a student class
we have student class this student class has three properties.
Rule number which doesn't have any value age which does not have any value.
And it will have total student.
Now let's say the value is 20.
Okay.
So too does student.
Now is a part of student class.
So we have this student class okay.
Now how can we access the role.
But okay.
So do accessibility.
But we need to give it our object.
Okay.
We can access rule number using an object so the accessible number we will write object name dot Roland
but via we need object because rule number is a non static property rule number is on static property
to access a non-strategic property.
We need object to us on order number of similarly installed order number.
And so on.
Okay.
But how can we exist to the student property so told us when property is a static property.
So if I if I will write if I tried to print the value S-1 Dodd told us to rent.
What will happen.
So obviously this line is wrong.
Why.
Because if we'll see it if you will see S1 so S1 does not have any property to the student.
So obviously this line will throw us at it.
Okay.
Because S1 does not have any property to the student.
So this line is wrong.
Now how can we access to the student property.
So total destroying property is a state property.
And I told you static property belongs to a class too.
If you want to access to truly stolen property we have to access it using glass.
So first we have to write last name student.
Then scope resolution operator and then the static property.
The name of district property.
So this is class name
this operator we call it scope resolution operator scope resolution operator and then the name of these
static properties so this is our static property total student.
Okay so this is how we access this is how we can access a static property okay using the glass using
the class.
Okay.
Now how do initialize our static property so static property can only be initialized outside the class.
Okay.
Static properties are initialized outside the class and the syntax is very simple.
We have to write in digit we have right integer here but we will.
But we again have to write in digit then the name of the class.
Then scope resolution operator then the static property name with a static property.
And then it's a value.
So this is how we initialize a class.
This is how we initialize a static property.
Okay.
So static property can only be initialized outside the class so letting good.
So first of all we have to make this property static property because this property is combined for
all the objects.
It is not it does not belong to us investment.
It belongs to a class.
Now I told you if you want to initialize a static property it can be initialized only outside the class.
So we have to write in beta again.
Okay we have to write and digit again.
Then the name of the class which is a student and then scope resolution operator column column then
the name of the property may not name of destructive property and now its value.
Okay.
So initialize
static get a member
okay.
So you have to remembered that we can only initialize distracting properties outside the class.
Okay now let us create some objects
student s 1.
Now what will happen.
Default constructor will be called.
Now let us try to print.
Now let us try to print it.
Rule number in each property so as one dot rule number we can access or no because inseparable property
and similarly let us try to print its age so our son daughter age and Lane Now let us try to print the
value of total student since 2 the student deserve a static property we will access it using the class.
So class name school resolution operator and then the name of the static property okay.
So what should be our output.
So at this line what will happen default constructor will be called so roll number and age board contain
the garbage value so garbage will be printed.
Then again garbage will be printed.
And what is the value of to the student.
So if we see this called the value of the students we initialize it to zero s but should be garbage
will look garbage value and zero.
Okay so let us run this file.
Now we will run this file
so garbage value garbage value and then zero 0 okay solver code is working fine.
Okay.
So everything we discussed is working fine.
Now what will happen if I write
if I am planning to access the code of student property using the object.
Then what will happen.
Okay so outline number 9.
What will happen what will be our output.
Okay so let us run our file.
Let us rent and see what will you add output
so the output is coming out to be 0.
It's very strange.
The output is coming out too busy to solve a code is working.
Campbell is not throwing us any at it but logically this is incorrect.
Okay logic it's incorrect but Campbell is allowing us to do this why it is logically incorrect because
student s one does not have any property with denim told us to rinse okay.
Now what will happen if I try to change the value of total students so S1 dot told us to rent.
Let's make it 20 Okay now let us create an ideal object.
Student s to and let us try to print the value of the students using the object as to so as to dot to
restaurant.
So what I'm doing here is I am trying to change the value of those students using the object S1 and
then I am creating a new object a new student S2 and then I am trying to print the total the value of
the student using the object as to so logically this line is wrong.
Why.
Because student 1 doesn't have any property with the name to the student.
Similarly this line is also wrong but compiler allow us to do this.
Okay
so I see the value of those students is 20.
If we accept that Telstra destroying property using the object as too so why does coming out with 20.
Let's see
so I told you the property to the student.
This is a static property.
It will not get copied.
So we have student class inside the student class.
We have this property to the students.
So there is only one memory block.
There is only one.
Okay.
It will not get copied.
So at this line I am trying to access to the student property so what.
Object Lesson will do.
It will go to class.
It will reach class.
It will lose history ruling class and it will try to redo its property.
So initially it was zero.
So without the zero then I am changing the value of total student so S1 it will reach the student class
and it will change the total Australian property which is it now becomes 20 --.
Then I am creating a new object as to as to draw total student so the object is to reach the class and
it will read the total student value which is twenty so twenty will get printed and similarly when I
am printing the value of total student which is 20.
Okay so we can change we can change the value of static property using any object okay using any object
we can change its properties but logically it's incorrect okay.
Logically it is incorrect but compiler allow us to do this.
Okay but we should not do this.
If you want to access the static properties accessed using the name of the class.
Okay so logically it is incorrect but compiler allow us to do this.
Okay.
Now let's see.
So as we discussed in the starting of the video.
So as we're discussing the starting of the video our aim is very simple.
As soon as we create a new object the value of the stolen property should be incremented by 1.
Okay.
If I create tools to rent then the value of p s total student property will they will.
If I create dense rent the value of the certain property will return.
Okay.
So how to do this.
So we know how to initialize to different how to initialize a static property.
We know how to access a static property but we do not know how to update a static property.
Okay so what we can do is we can.
We can take the help of a constructor.
Okay.
So what will happen whenever we create our object constructor will be called and inside the constructor
we will write to those two rent plus plus.
Okay so I am planning to create a constructor so we know when the object will be created constructor
will be called and inside the constructor we will write to the student plus press.
Okay.
So in this way the value of clustering the property will be incremented okay.
So let us delete all this called.
Now let us create a student so a student has to now let us create more students.
Student as 3 as food as 5.
So after creating face to rent I am trying to print the value of total student.
Okay so what should be our output.
Let's see.
Okay so why is this coming out to be zero.
Because after doing changes to this file after doing industrialist failed before God to this file.
Now we will read this file.
See no doubt produce coming out to be 5 via 5 because because first the constructor is called Forced
to index 1 so told us restaurant become 1 then the constructor is called for this too.
So the value of Telstra and property becomes two and then the constructor will be called for student
S3 as food and is 5.
So in this way the value of total student property became 5 okay.
So just like that starting with the members we can make functions statically so okay we can make static
functions also so suppose I have a function and to get William but
I a function get on them.
But so what this function will do it simply will return the roll number
okay now dysfunction get really embodies object dependent function.
This function depends on the object and object will like this function.
So by default this function is an offset to function.
So by default every function and by default everyday demand men but they are non static.
Okay.
So by default this is non static since this function is known instead start again object will call this
function S1 dot getter William but astronaut good William.
But still we need an object to call this function okay.
Now what I want to do is now let's make this property a private property now since this property is
a private property.
We need a getter function to access this property.
So let us create a getter function.
So in get to those rent
to what this function will do it will return to restaurants Okay so we will make this function as a
static function.
So after writing The Giver static.
Now this function belongs to a class object will not be able to call this function.
Okay so S1 dot get to the student astronaut get restaurants.
These are wrong.
How we will call this function so I told you this function belongs to a class so does index to excess
that start to function is the name of the glass school resolution operator.
And the name of the function name of the static function which is get total student.
So this is how we will call this function if you will write as DA to get to those rent.
Now this is logically wrong why this is logically wrong because this function is a static function and
static means.
This belongs to class and not to an object okay.
Now there are a few properties.
There are a few important things that we should know about the static functions.
Okay so few properties of destructive function nodes.
So the first thing.
So the first thing is static functions can only exist static properties whether it is a data member
or a function.
Okay so the function can only exist static properties whether it is indeed a member or a function.
We cannot use non-strategic function.
We cannot use non-strategic the member inside a static function.
And also we cannot call non-strategic functions.
Okay so static function
can only access static data members start to the members and can call only static functions.
So for example get told us when this is a static function.
So if you will right here return age then this is wrong.
Error will be added because the first property of static function is the can only exist static data
members and they can only call static functions okay.
Now the second property of the static functions are static function do not have access do not have discovered
static function do not have discovered.
Okay because we do not call static function to an object.
So what happened is this is a normal function.
So to call this normal function we need an object.
So as one dot get rule number so this object will be stored in that this keyword of this function.
Now when we have static function so for calling the static function we do not need an object since we
do not need an object.
We do not need this operator.
Okay.
So these are the two very important properties of static functions first static function can only have
access of the static grid members and they can only call static function.
Second properties starting function do not have this keyword because we do not use object to call these
functions.
Okay.
So this is all about static properties of a glass.
We can make a data member.
Static and we can also make functions static okay.
Static means these properties belongs to a class and not to object.
Okay thank you.