Well discussing your neighbors B-ble if you discuss tactic with him as a no let alone little bit more 2
about them. 3
We not only shut them all very relaxed and our student class to incorporate static variables. 4
Like startlement that's to clear the books are also declared with the key word stocktake and it means 5
that there's a topic to the class that is there independent of any instances of the class like Instance 6
variables static variables are also declared directly with the necklace that is a class level. 7
So you can declare them and methods and you will get compiler dollars if you declare them and Mather's 8
also like Instance variables they get initialized with a default value. 9
If they are not explicitly initialised not the static variable is declared with a public access modifier 10
then any code that is external to the class can access it. 11
We are that class in of course code inside the class can access it directly. 12
Also if it has default access level we know that only classes within the same package can access that 13
variable so just ecstatic my first class name followed by dot operator is used to access and this topic 14
really was declared in that class since I think we're able to start a class they're also referred to 15
as class variables. 16
So there is only one copy of a static variable. 17
But class that copy is shared across all objects of the class what this means is if one object updates 18
the value of a static variable then all other objects will see the same object value. 19
On the other hand with Instance variables we know that every object of the class gets its own copy of 20
every instance variable declared in the class so instance variables hold values unique to each object 21
but does nothing but the spirit of the object. 22
Here's a simple example here we have the Student class with the static variable called Con which is 23
initialized to zero. 24
There are also three instances of the Student class named S1 S2 S3. 25
So as one increments can then count will be 1 and 2 and 3 will see the same or better value 1. 26
Now if S2 increments can then convert increment or to do an as one industry will also see the contract 27
us to not count is declared with public modifier then any court extended to the Student class can also 28
include incremented and that a better value will once again be visible to any court that is able to 29
access the and variable so static variable values are truly global. 30
That is there is only one copy of the static variable per class not come into accessibility of static 31
variables within the class in which they are declared they're accessible from static method to defining 32
the class as both of them are started with a class but since they're also shared across objects of the 33
class they are also accessible from instance methods as well as concepts of the class. 34
Although we discussed accessibility of instance and static variables from the different kinds of methods 35
like static on instance then must all be some confusion. 36
So to be 100 percent sure. 37
Let's look at this example had the class to start degradable and one instance variable. 38
We also have two methods one instance method and one static method an instance mother can access anything 39
that is topic for instance. 40
So as you can see the instance method can access the instance really the static variable and also the 41
static method. 42
So basically there is no restriction when it comes to instance methods nobody comes to a static method 43
we know that it needs to stay away from anything to do with objects that so as you can see it can access 44
the static variable as it is not object specific but we get a compiler error when it Kreiss to access 45
either the instance variable or the instance method as their object specific however the final statement 46
shows that it can access an instance method or even an instance variable enough object to that object 47
reference that is perfectly fine. 48
Like we see here the object can be an object of the same class in a static matter it is different. 49
It is just that the static method cannot directly access any data related stuff declared in the scene 50
class let's make a small enhancement below a student class example to this topic really both in action 51
you can also find the biggest quote in the resources section let's in enhance our student example a 52
bit more. 53
Let's say we want to keep track of the number of student objects getting created. 54
So there should be a counter which gets incremented every time a student object is created. 55
Begin create the counter as a static variable. 56
So let's create a static variable call student called 57
Write topic and that's just naming does student count and let the default be zero and here in the constructor 58
let's just say student called less bliss unlist just hard to get there for the student called 59
does get student 60
can actually make it static 61
and let's just go into the Student class method and let's say 62
number of students created so far and you can simply do this to that class name and get student 63
which is a static method static methods can be worked directly with the class name. 64
We already know that said. 65
So let's just go ahead and compain her on it and it says number of students created so far is three 66
and that's said Now can you guess what happens if it if we remove the static keyword Yes and also make 67
this this. 68
In this case it's an instance variable. 69
So it is specific to the object. 70
And when we increment it for each student object it just gets incremented from 0 to 1 and that's about 71
it. 72
So it is incorrect right so it's not going to keep track of the total number of students that are getting 73
created. 74
So it just keeps on incrementing from 0 to 1 and that's incorrect. 75
So it has to be static and it's true we started and that's about it.