Static Keyword - Part 2

9 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
€85.19
List Price:  €119.27
You save:  €34.08
£74.77
List Price:  £104.68
You save:  £29.91
CA$137.78
List Price:  CA$192.89
You save:  CA$55.11
A$150.31
List Price:  A$210.44
You save:  A$60.13
S$129.19
List Price:  S$180.88
You save:  S$51.68
HK$777.80
List Price:  HK$1,088.96
You save:  HK$311.15
CHF 79.59
List Price:  CHF 111.43
You save:  CHF 31.83
NOK kr1,013.63
List Price:  NOK kr1,419.13
You save:  NOK kr405.49
DKK kr636.11
List Price:  DKK kr890.58
You save:  DKK kr254.47
NZ$172.27
List Price:  NZ$241.19
You save:  NZ$68.91
د.إ367.21
List Price:  د.إ514.11
You save:  د.إ146.90
৳12,200.05
List Price:  ৳17,080.55
You save:  ৳4,880.50
₹9,057.54
List Price:  ₹12,680.92
You save:  ₹3,623.38
RM409.68
List Price:  RM573.58
You save:  RM163.89
₦144,900.08
List Price:  ₦202,865.92
You save:  ₦57,965.83
₨27,977.78
List Price:  ₨39,170.01
You save:  ₨11,192.23
฿3,144.12
List Price:  ฿4,401.89
You save:  ฿1,257.77
₺4,257.57
List Price:  ₺5,960.77
You save:  ₺1,703.19
B$541.88
List Price:  B$758.66
You save:  B$216.77
R1,687.37
List Price:  R2,362.39
You save:  R675.01
Лв166.64
List Price:  Лв233.31
You save:  Лв66.66
₩147,438.25
List Price:  ₩206,419.45
You save:  ₩58,981.20
₪322.26
List Price:  ₪451.18
You save:  ₪128.91
₱5,910.90
List Price:  ₱8,275.50
You save:  ₱2,364.60
¥15,573.94
List Price:  ¥21,804.14
You save:  ¥6,230.19
MX$1,801.20
List Price:  MX$2,521.76
You save:  MX$720.55
QR363.83
List Price:  QR509.38
You save:  QR145.55
P1,322.75
List Price:  P1,851.91
You save:  P529.15
KSh12,873.43
List Price:  KSh18,023.31
You save:  KSh5,149.88
E£4,730.87
List Price:  E£6,623.40
You save:  E£1,892.53
ብር15,598.69
List Price:  ብር21,838.79
You save:  ብር6,240.10
Kz91,690.83
List Price:  Kz128,370.83
You save:  Kz36,680
CLP$91,270.93
List Price:  CLP$127,782.96
You save:  CLP$36,512.02
CN¥705.37
List Price:  CN¥987.55
You save:  CN¥282.18
RD$6,346.47
List Price:  RD$8,885.32
You save:  RD$2,538.84
DA12,964.71
List Price:  DA18,151.11
You save:  DA5,186.40
FJ$227.15
List Price:  FJ$318.02
You save:  FJ$90.87
Q764.64
List Price:  Q1,070.52
You save:  Q305.88
GY$20,885.90
List Price:  GY$29,241.10
You save:  GY$8,355.19
ISK kr12,616.33
List Price:  ISK kr17,663.37
You save:  ISK kr5,047.03
DH918.43
List Price:  DH1,285.85
You save:  DH367.41
L1,687.62
List Price:  L2,362.74
You save:  L675.11
ден5,239.78
List Price:  ден7,335.90
You save:  ден2,096.12
MOP$800.52
List Price:  MOP$1,120.76
You save:  MOP$320.24
N$1,684.29
List Price:  N$2,358.08
You save:  N$673.78
C$3,674.23
List Price:  C$5,144.07
You save:  C$1,469.83
रु14,445.11
List Price:  रु20,223.73
You save:  रु5,778.62
S/336.11
List Price:  S/470.57
You save:  S/134.45
K430.33
List Price:  K602.48
You save:  K172.15
SAR375.18
List Price:  SAR525.27
You save:  SAR150.08
ZK2,303.63
List Price:  ZK3,225.17
You save:  ZK921.54
L433.54
List Price:  L606.98
You save:  L173.43
Kč2,066.70
List Price:  Kč2,893.46
You save:  Kč826.76
Ft32,660.30
List Price:  Ft45,725.73
You save:  Ft13,065.42
SEK kr926.89
List Price:  SEK kr1,297.69
You save:  SEK kr370.79
ARS$143,373.53
List Price:  ARS$200,728.68
You save:  ARS$57,355.14
Bs689.81
List Price:  Bs965.76
You save:  Bs275.95
COP$380,209.72
List Price:  COP$532,308.82
You save:  COP$152,099.10
₡49,937.50
List Price:  ₡69,914.50
You save:  ₡19,977
L2,628.31
List Price:  L3,679.75
You save:  L1,051.43
₲670,577.05
List Price:  ₲938,834.69
You save:  ₲268,257.64
$U3,917.97
List Price:  $U5,485.31
You save:  $U1,567.34
zł359.54
List Price:  zł503.38
You save:  zł143.83
Already have an account? Log In

Transcript

Hello all welcome back to the next one. And this one, we will talk about some kind of restrictions or key points about static variables and methods. So let me just bring up Eclipse and we will understand them again from the code perspective. So it's easy to figure out and actually understand the concept rather than the theory. So there are two main restrictions for static method. I'll first talk about the restriction and then I'll explain from the code perspective.

So, the first one is that the static method cannot use a non static variable or method directly and a second one, so let me just maybe, let me just write it. So you will remember it. So static, methods method cannot be used. Non static variable or method directly. So, this is the first one the second one is this an slope super cannot be used in static context. This is pretty easy so, when we have the second one is very easy to understand when we have the inherited classes or the the this keyword when we use for this keyword is pretty obvious like last time we took a look at these classes.

Now, when we have the instance number as the static method, we should not be using the this keyword here right. We should not be using the this keyword because this will throw an error. This is always related To the object of the class, and we learned that the static variable is not related to the object it is associated with the class itself. So, this one is clear, it is explained that we cannot use that this keyword. Similar way, we cannot use the super key word with these static variables or static methods. The important part is to understand the first one, which is static method cannot use the non static variables or methods directly.

So, now let's try to understand it. I'm going to define a class variable up here, so public, in fact, I'm going to say maybe add num equals to 10. And this is a non static variable. And you can see that right. And let's define one more method here, which is going to be public and in some And what I'm trying to do here is it is gonna accept one integer, and I'm naming it as num. And what it's gonna do here is it's gonna return num plus add num.

So what we're doing here is wherever we call this method called sum, we're going to take a number integer number from there, and it's going to add this 10 from the Add num variable, and return the final sum. simple method doesn't do a lot of stuff, but good enough to understand the concept. So now, within our main method, I want to do this thing in output equals to, I'm trying to call the sum method, and since it returns an integer type, we need an int output. So sum, and I'm providing 10 or maybe let's say I'm providing 20 to it. And this Let me just print the output also. So see, so the output is output.

But now what we see here is we are getting an error. And let's just read it, it says, cannot make a static reference to a non static method, some end from the static type static keynotes demo. So static keynotes demo is our class name. That's what I named it. And it says that it cannot reference to the non static method some. So non static method some is this.

What it's saying here is the main method is also a static method. We cannot call a non static method directly into a static method. That's what it's complaining for. Do you see that right And if and you know what, let me just do one more thing. I'm just going to create one more method. And I'm gonna say stats, some static, something like that.

I'm just, I'm just making up a name, please name it anything. But I'm going to make this method as static method. So now we have a static method. And if I access it here in the output, or maybe, I would say static output. So that we can try to see this. And I'm trying to access this one some static.

And I'm gonna call 10 here, and then. So you will notice that we don't see any error here because this method is static. And main method is static. So it does not have any issues accessing the some static, but now we see a different error here. And it says cannot make a static reference to a non static field add num. So what happened here is this method, some static is a static method, but this add num field this variable that's what it's complaining for.

Again read the error. This is a non static field add num, so this guy is non static, we did not make it a static static field, right? So that's why it's not able to read it, it's not able to use it. So if we make this field as or maybe let's create one more variable, and let's call it as a let's make it a static field. So public static and add num static. I'm just adding the static keyword here so that we know that this is the static variable that we define.

And here if I say static It's gonna be more than happy now, boom, we will not see any error here, we don't see any error here also. So, it means that in a main method or in a static method, we cannot use the non static fields we cannot call a non static method also in a static method. So, this is what it means. Now, the question is What if I really want to use this sum method here in the main method, what should I do, then what do we have to do here is in the same class itself we will have to create the object of the class. So, like this, I would say s one equals two new and object off the same class. And then here I can do s one dot sum and this will make it make it happy.

Now, there is no issue and this one this guy is giving me that the local battery variable is not used, so we can simply make it happy by using it. I'm just gonna say here that use the static output and this is also going to be happy. So, what we have done here is we have seen that we cannot access the non static fields within a static method and we have also seen that, how do we access it, if we really have to access it right we can create objects within the same method. So, this was really important, we wanted to understand how static and non static math methods and variables relate to each other, how do we access them? So class variables, one map one very important thing, these class variables or methods also these methods, they don't really exist unless the class is executed when the entry point is main method.

And these variables do not exist unless the class is executed unless the class is low. In the JVM, the Java Virtual Machine, right, and an instance is created. So once the class is loaded, one instance of the class is created in the JVM. That's why the main method cannot call the class variable or method, because main unless this is executed, there is no instance. And if there is no instance, the non static fields and variables cannot be called in the main method. This is why this limitation exists.

This is why it only it can only call the static variables or methods. So that's about it. I'm sure it's gonna be a great thing, because this is really important to understand the difference between static and non static, and how do we actually access them? So please download this code. And let me know if you have any questions I'll see in the next lecture. Thanks for listening.

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.