Static Keyword - Part 1

14 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€92.30
List Price:  €129.22
You save:  €36.92
£79.14
List Price:  £110.81
You save:  £31.66
CA$135.92
List Price:  CA$190.30
You save:  CA$54.37
A$153.17
List Price:  A$214.44
You save:  A$61.27
S$134.71
List Price:  S$188.59
You save:  S$53.88
HK$782.26
List Price:  HK$1,095.19
You save:  HK$312.93
CHF 90.56
List Price:  CHF 126.79
You save:  CHF 36.22
NOK kr1,076.34
List Price:  NOK kr1,506.92
You save:  NOK kr430.58
DKK kr688.47
List Price:  DKK kr963.88
You save:  DKK kr275.41
NZ$166.49
List Price:  NZ$233.09
You save:  NZ$66.60
د.إ367.19
List Price:  د.إ514.08
You save:  د.إ146.89
৳11,000.23
List Price:  ৳15,400.77
You save:  ৳4,400.53
₹8,335.05
List Price:  ₹11,669.40
You save:  ₹3,334.35
RM473.25
List Price:  RM662.57
You save:  RM189.32
₦139,656.03
List Price:  ₦195,524.03
You save:  ₦55,868
₨27,872.43
List Price:  ₨39,022.53
You save:  ₨11,150.09
฿3,637.73
List Price:  ฿5,092.97
You save:  ฿1,455.24
₺3,222.80
List Price:  ₺4,512.05
You save:  ₺1,289.25
B$497.67
List Price:  B$696.75
You save:  B$199.08
R1,888.37
List Price:  R2,643.79
You save:  R755.42
Лв180.67
List Price:  Лв252.95
You save:  Лв72.27
₩134,942
List Price:  ₩188,924.20
You save:  ₩53,982.20
₪366.06
List Price:  ₪512.51
You save:  ₪146.44
₱5,625.18
List Price:  ₱7,875.48
You save:  ₱2,250.29
¥15,118.78
List Price:  ¥21,166.90
You save:  ¥6,048.12
MX$1,663.06
List Price:  MX$2,328.35
You save:  MX$665.29
QR365.44
List Price:  QR511.63
You save:  QR146.19
P1,373.02
List Price:  P1,922.28
You save:  P549.26
KSh13,148.68
List Price:  KSh18,408.68
You save:  KSh5,260
E£4,723.53
List Price:  E£6,613.14
You save:  E£1,889.60
ብር5,694.53
List Price:  ብር7,972.57
You save:  ብር2,278.04
Kz83,612.73
List Price:  Kz117,061.17
You save:  Kz33,448.44
CLP$98,024.19
List Price:  CLP$137,237.79
You save:  CLP$39,213.60
CN¥722.49
List Price:  CN¥1,011.52
You save:  CN¥289.02
RD$5,928.03
List Price:  RD$8,299.48
You save:  RD$2,371.45
DA13,469.95
List Price:  DA18,858.47
You save:  DA5,388.52
FJ$225.25
List Price:  FJ$315.36
You save:  FJ$90.11
Q781.84
List Price:  Q1,094.62
You save:  Q312.77
GY$20,987.69
List Price:  GY$29,383.60
You save:  GY$8,395.91
ISK kr13,817.61
List Price:  ISK kr19,345.21
You save:  ISK kr5,527.60
DH1,010.57
List Price:  DH1,414.84
You save:  DH404.26
L1,763.81
List Price:  L2,469.40
You save:  L705.59
ден5,677.01
List Price:  ден7,948.05
You save:  ден2,271.03
MOP$807.66
List Price:  MOP$1,130.76
You save:  MOP$323.09
N$1,895.98
List Price:  N$2,654.45
You save:  N$758.46
C$3,689.21
List Price:  C$5,165.04
You save:  C$1,475.83
रु13,357.88
List Price:  रु18,701.57
You save:  रु5,343.68
S/371.02
List Price:  S/519.45
You save:  S/148.42
K383.60
List Price:  K537.05
You save:  K153.45
SAR375.02
List Price:  SAR525.04
You save:  SAR150.02
ZK2,548.47
List Price:  ZK3,567.96
You save:  ZK1,019.49
L458.83
List Price:  L642.38
You save:  L183.55
Kč2,334.38
List Price:  Kč3,268.22
You save:  Kč933.84
Ft36,425.52
List Price:  Ft50,997.18
You save:  Ft14,571.66
SEK kr1,059.50
List Price:  SEK kr1,483.35
You save:  SEK kr423.84
ARS$85,666.43
List Price:  ARS$119,936.42
You save:  ARS$34,269.99
Bs695.09
List Price:  Bs973.16
You save:  Bs278.06
COP$389,673.99
List Price:  COP$545,559.18
You save:  COP$155,885.18
₡50,509.65
List Price:  ₡70,715.54
You save:  ₡20,205.88
L2,474.33
List Price:  L3,464.17
You save:  L989.83
₲736,638.54
List Price:  ₲1,031,323.43
You save:  ₲294,684.88
$U3,781
List Price:  $U5,293.55
You save:  $U1,512.55
zł398.17
List Price:  zł557.46
You save:  zł159.28
Already have an account? Log In

Transcript

Hello all welcome back to the next one. In this one we will talk about the static keyword in Java. So static field or static variables methods, they are always associated with the class instead of the class instance. And class instance is the object, right. So static variables, they're always associated with the class itself, not with the object of the class. So let's talk about it in terms of coding, we will do a small coding exercise and we will understand how, what exactly it means.

So I created a package called static package, I always add something because static is already a keyword in Java. So I do not want to name this package as static. I added static package or add something but never use the keywords which are already existing in Java. Then I created two classes here, static demo, which is the class which has Main method, and then another classes static example class. So let's, let's start, what I'm going to do here is I'm going to first define some class variables. So private string, and maybe let's call it a make.

So let's talk about the cars the makes of them. And then private. And instance num. So this is the number of instances created for the class. And this is a variable that I'm defining specifically to explain the static context. And we will use it in some time.

So let's go ahead. Now what I want to do here is public. And then I want to use the same name. What I'm doing here is I'm creating a constructor for the class and string make and in this we will say this dot Make. So this is how we use this keyword and define the constructors and all that right. And here we will say, Make.

Inside the constructor, what I want to do here is I want to actually increment the instance num. So now I'll explain it. What it is. It is the number of instances created for the class. So let's say if I create two instances of this class, the static example class, then I'm trying to say that the number of instances should increase right? Currently, I'm making it zero.

But if I create the funds first instance, then it should increment by one, right? So instance, num plus plus should increment it by one. When I created the second instance, it should increment it by one again. So it becomes two, right? So this is the kind of goal we have right now. And I'll I'll explain it.

So now let's create like one more getter methods. So public string and get make and this should just return done the make or the start make or anything you can do. So I don't make and public and get instance. So this is the this is the very important method and this is something we got to use to actually explain the concept. So now it should return the instance numb, right. So, this is the class we created, and we're going to use it in the static demo where we will create the object for this class.

And we will use these field variables or variables in that class to explain the concept. So, inside the main method, static and example class This is the name of this class static example Class II, s one equals to new static example class. And it needs to make in the constructor, so I'll give something like maybe BMW. And this is the first object we created. And I'm going to type some print statements here. So let me just do that.

And the first one we will do here is make off car is so we're setting the make from the constructor right here. And then we're going to access the make and verify if it was set successfully. So s one dot the object we are accessing s one dot get, get make. So this method should give us the make and then we will say this is So, we want to find out how many instances are created. So this is instance number and we will get the instance number. So s one dot get instance number, right this is the method we created to get the instance number remember.

So in the constructor we are setting the make, and we are always incrementing the instance number. And then here we are trying to print if what is the instance number of this object right. Now, I'm going to again, copy the same code block and put it here, make it as two and maybe change the make two pens and then use it as s two. So what I'm trying to do here is I'm trying to create two objects. So we can see if we're getting the correct information back from the class static example class. So we have the example Same thing now let's run this code and see what is the output.

So when I run this code I see make of the car is BMW This is instance number one which is correct the first instance as one. Second time I see make of the car is bent, which is correct. But again it says here, this is instance number one, which doesn't look like a correct thing, right? Because, according to us, this is instance number two and why we are still getting the instance as one. So, now let's see that. So, the reason is because this is these are field variables on the class and they always belong to the object of the class.

So when I created s one, it has said the value of make to BMW and it has said the value of instance two plus plus from zero and it's done to one But when I create a new object called esto, it again sets the value of make. And for the instance also, it again starts from zero right here. And it increments from there, and it makes it one. So it does not keep track of what happened before or it's not about keeping track. It's not the same instance, this is completely different in the memory. That's why Java is actually treating it as two objects and two different references are saved in the memory.

And every object is accessing its own values, right? This s one is accessing BMW and this instance number which is obviously returning plus one, because it's zero plus one, and the second object as two, it's also behaving the same way it's saving the make as Ben's and instance number again, saving as one because it also again, starts from zero and it increments it to one this is Reason. So now the concept behind static is if we want to attach something, something to the class and not to the object so now if you understand these things are attached to the class object, if we want to attach something to the class, we want to define it as static. Now, let me just make the change quickly and let's show you so I'm going to make it as private static in this time. So now this variable the instance number, it has become in a static variable.

And then let's quickly run the code and let's figure out the difference. There you go boom. So make of the car is BMW instances one and in the second one make of the car is Ben's and the instances to now you see the difference. What happens is when we make the variable as static, the variable is now a class variable. It's not an object variable, it is not attached to the object instance it is attached to the class itself. So if you declare any variable as static, that is known as a static variable, and the static variable can be used to refer to the common properties of all the object.

So if you have any krump common properties, you want to define those properties as objects. And those like common properties means that is not unique for each object, they should not be unique for the object like they're common for every single object created from that class. That variable should be defined as static. For an example, what we can do here is let's say, I'm assuming that all cars right now has four wheels. I know there could be exceptions, but let's stick to the common thing. So what we can do here is from this class point of view, can define something like private, in, private static in and we count.

So this is an example in this format. So maybe they all have the four wheels, right? So I can define this variable as static. Because even if I create an object for BMW or bands or Audi or Honda, I'm assuming that all of my cars are going to have four wheels. So this variable does not change with the type of the car object that I'm creating. Whether I create bmw, bmw or Benz, I will always have four wheels.

So I can define this variable as static. Let's assume that you have a student class and all these students that begin to create are studying in the same college. This is one example right? So for the student class, We can have information about all these students like in name of the student, which semester the student is studying, roll number, address, phone number, anything like that, right? But they all those students are studying in the same university or same college. So for the college name, we can have a static variable defined, so that we don't have to define the college name for every single object or instance, same thing, let's say we have a class for employees, but all the employees are in the same company.

So the company name is going to be same. So please understand the concept just give it a thought. Any common property, any common stuff can be defined as static, because we do not want to change it along with the instance of the class. Right? So this is what it means. And what does it mean and how does it help it means that it will only have one copy After variable in the memory at the time of class loading, so when the class loads, it will only have one copy in the memory that variable.

So whether we are accessing it from two different objects, they both the objects are accessing one single copy. So it saves memory. And it saves execution time, right. So all the instances of the class will share that same variable. This is this is the key. So, now, since we know that the the says the instance number here, it's a static variable, we don't need to use the object name.

What we can do here is we can use the name of the class so static example class and static example class here, and now we get an error and let's figure that out. So, what it says is, cannot make static reference to a non static method, get instance number. So, what it says is You are trying to call a method with the static reference because you are not using the object you're using the class name. But this method, the get instance number is not defined as static. This is still non static, right. So what we need to do here is public static and get number.

Once we know that this method also becomes static, and it uses the static variable, and this thing is also happy. So this is important because we're accessing static variables. So it's good to make the variables sorry, the methods also as static. So if we're working with a method which involves static fields, then it makes sense to make that method static. And if we make the method static, then we do not have to use the class instance to access the method right we are not using the class instance now we are using the name of the class. So this is what the static means.

And that's about it. So we will go in a little bit more concepts about static in the next lecture. For now, please download both the code files and try to exclude them. And let me just first run them random ones so that you finally have a output. So make of the car is BMW, this is instance number one make of the car expense, this is instance number two. Now this static class is used to access these methods.

And it is belonging to the class not to the object, right? So that's about it. Please go through it. Practice as much as you can. Let me know if you have any questions and I'll see you in the next lecture. Thanks for attending this one.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.