Default Variable Values

Complete Step By Step Java for Testers Java Concepts - Data Types
8 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.76
List Price:  CA$192.87
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$778.05
List Price:  HK$1,089.31
You save:  HK$311.25
CHF 79.75
List Price:  CHF 111.66
You save:  CHF 31.90
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.26
List Price:  NZ$241.17
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
₦145,242.47
List Price:  ₦203,345.27
You save:  ₦58,102.80
₨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,269.18
List Price:  ₺5,977.02
You save:  ₺1,707.84
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.49
List Price:  Лв233.10
You save:  Лв66.60
₩147,476.25
List Price:  ₩206,472.65
You save:  ₩58,996.40
₪322.26
List Price:  ₪451.18
You save:  ₪128.91
₱5,910.90
List Price:  ₱8,275.50
You save:  ₱2,364.60
¥15,574.94
List Price:  ¥21,805.54
You save:  ¥6,230.60
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,753.52
List Price:  E£6,655.12
You save:  E£1,901.60
ብር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,015.89
List Price:  CLP$127,425.89
You save:  CLP$36,410
CN¥705.37
List Price:  CN¥987.55
You save:  CN¥282.18
RD$6,384.36
List Price:  RD$8,938.36
You save:  RD$2,554
DA12,968.10
List Price:  DA18,155.86
You save:  DA5,187.76
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,638.73
List Price:  ISK kr17,694.73
You save:  ISK kr5,056
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.35
List Price:  ден7,335.30
You save:  ден2,095.95
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
K425.87
List Price:  K596.24
You save:  K170.36
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,795.72
List Price:  Ft45,915.32
You save:  Ft13,119.60
SEK kr926.89
List Price:  SEK kr1,297.69
You save:  SEK kr370.79
ARS$144,060.59
List Price:  ARS$201,690.59
You save:  ARS$57,630
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ł360.46
List Price:  zł504.66
You save:  zł144.20
Already have an account? Log In

Transcript

Hello, all welcome back to the next one. So we will continue from where we left off actually, and I want to show you a couple of more things in variables. And if you remember, we talked about the default values quite a lot. And I'm just gonna copy this file and rename it maybe like, variables, default, or maybe like default variable values. So something like that. So now I have created a new class, and I've just copied it.

So it's exactly the same class. I'm going to remove these comments from this, make it a little shot. And what I wanted to show you here is right now we are assigning values to it right? So what if I don't want to assign a value and we want to see the default values, right? How can we use the default values? So let me just show you.

You will see that just I deleted the assigned value and it starts showing me some errors and it says local variable byte variable may not have been initialized. So what happens is Java recognizes then Eclipse tells us that this variables value may not have been initialized. So this is known as declaring a variable. Giving it a value is known as initializing a variable. So, when we initialize it, it's happy when we don't initialize it. It's not happy.

It says, You have an issue. And you know what, it's a compile ation issue. It's right here. So it's not even gonna work. Like even if I try to run it. It's just gonna say errors exist in the required project.

Do you still want to run it? And I can say, go ahead, proceed. But I'll still see this unresolved compile ation problem. So which means that there's no point running this code if we see something like that if we see this kind of error. Let's just try to fix it before running it and now You may ask like, okay, dude, when I actually removed the value, then it's actually it's kind of giving me error. So, how do I use the default default values?

So, let me just do that. Let me remove these few more comments. So, we make this code more more fit on the screen and then let's see. So, we can actually, the concept is we can leave a local variable without defined without initializing it. So, Java will complain. So, how we can verify the default values of these variables is we can define them globally.

So, one concept is that if some variable is defined in the method, So currently, this is the main method and do not worry about these methods and these terms, I have covered methods and classes in a lot of detail in future lectures. So you will, you will understand what a main method is there. You will understand what a different method is there so do not worry about it. So right now what I'm trying to say here is if you define a variable inside a method, and a Java class can have more than one method. So if I have a method here, let's say let's quickly talk about this thing, public and static, and do not worry about the static and all these, it's all covered in future lectures. So public static, and one more method, maybe just like that.

And if I try to access any of these variables, let's say, let's say I want to use the integer variable, or maybe Yeah, so this guy, I actually will not be able to use it even if I want to assign a new value to it. I will not be able to use it. What's gonna happen here is it's gonna say, integer value, integer variable, just read this error cannot be resolved to a variable because it doesn't even know about it. This scope of This variable this integer variable is within the main method. If I use it here, see I'm using it here, there's no complaints. But if I copy the same statement in this method, if I copy the same statement here, it's going to complain because it doesn't know where it is a variable is the scope is limited and the scope is limited to the method where the variable is defined.

Now, the other thing is, local variables are not allowed to be left without assigning a value it's complaining when I'm using it right. What we can do here is to show the concept we can make them as global variables. So anything that we dis define or declare outside the methods like in the classes, they become global variable and you will see the color also change right. So these global variables they can actually be left without initializing and should be okay. So Oh yeah, because we have to change them to static that is of this i. So, please do not worry about the static it is cover in couple of lectures very detailed explanation is there.

So because this method is static for now, please understand this thing that, because this main method is static, the outside variables that we are trying to use inside this method should also be declared as static. So, let's define them outside the main method. Let's define it here. So static and we'll remove the values that we assign. So, we want to use the default values and we want to see the default values of these variables right. Now, floor is there.

So let's put float here and remove the assigned value that we gave. So now ballasts there. So let's put the devil here. And it should also be static and remove the value from here, only couple of them left. So we should be good, we should be good. And this is their static and remove the value that we assigned.

And cat also so let's get rid of it. And now let's declare it here, make it static. And let's remove the value from here. So that's about it. This is and what we have done is we have moved all the variables declaration outside the class, outside of the method inside the class. And now we can access it and if you will notice since this variable became a global variable, this method is also not complaining it is fine right now.

Let's see the error that we are getting here. So it says set method type to void because we have actually not used. Like here, we have not used anything for the return type, so void and this is also covered. So don't worry for now we just want to understand and focus on variables. So do not worry about any of these concepts on methods static void. So, let's just run this code, and let's see the output.

So run as a Java application. And there you go. So byte value default 000. Short, zero and zero long zero float is zero dot zero double zero dot zero. Boolean is false for character, I think it's like empty space or something. So that's about it.

So these are the default values we can verify them. But if I define I declare the variables inside the main method, then I will not be able to run this program. I will not be able to run this program. If I Do this. And if I comment this code out, I'll start having issues. I'll just say, not official.

So right now, right now, there are different issues. So but yeah, the idea is, if we want to actually see the default values, we can declare them outside. But usually, we always declare the variables within the method where we make use of global variables is very low, when we really need something global. So this is not a practice, please do not follow it. This is something that I want to show you to actually display the default values that we talked about in the last lecture. So that's about it just for the understanding.

And just to show you that if I declare a variable inside a method, I cannot use it outside of that method. Right. So that's about it. Please stay tuned. Stay tuned for the next lecture, and I'll see you there. Thanks a lot 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.