Java Fundamentals Variable Scope
Transcript from the "Variable Scope" Lesson
>> Now let's talk about variable scope. So I mentioned this a little bit earlier, but the variable is only accessible within the scope that it is declared in, all right? So we're gonna write another program that approves anyone who makes more than $25,000 and has a credit score of 700 or better, and is going to reject all others.
[00:00:25] So we saw a little clip of this program, we're gonna actually write it out. We're ready for that now. Okay, so we'll create a new class. And we'll call this class Instant Credit Check. How are you doing with your cases? Good, all classes are beginning with capital letters?
[00:00:54] Perfect, all right. So, All the classes we've made so far, the first thing we've done, we've made our main method, right? We're getting a little bit more advanced now, that doesn't have to be where we start. We can even define variables at the class level, all right? So let's do that now.
[00:01:14] We're gonna started with static and the data type of the first variable will be an integer, and we'll call this one requiredSalary, 25,000, okay? And let's make another variable, static int requiredCreditScore, and this will be 700. All right, now, let's go ahead and make the main method, and we'll just go ahead and get our input.
[00:02:00] So we'll call some methods. We'll say double salary =, and we'll write a new method called getSalary(). Now what I like to do is I like to flesh out my thought process. And then I'll create whatever I need in order to meet that. But if I go and create that getSalary method, I'm gonna come back and say, now what was I doing and what's my next step?
[00:02:25] So I'm gonna write this, you'll see some red, that's okay, all right? Now I wanna get the credit score and so that'll be another method that we'll create called getCreditScore. We're going to say public static double getSalary And we'll ask for their salary. All right, things are gonna get really juicy.
[00:03:04] And now we want to input the salary. Now, I can define the scanner here and I can input that. But then when I go to my getCreditScore method, I'm gonna have to do the same thing, right? Because if I define the scanner, I'll do so, don't type this.
[00:03:28] Or you can if you want. [LAUGH] All right, so if I define this here, And then, I'm gonna make the other method just so you can see. You can go ahead and type this one too, getCreditScore(). Now, if I tried to say scanner., nothing is appearing here, or no methods, right?
[00:03:57] It doesn't know what I am talking about. Who is scanner, is what it's really saying, right? Let me try to say nextDouble or something we've been using. Notice scanner is red and it's saying, I can't resolve this symbol, meaning, I don't know what you're talking about. That's because scanner was defined in the getSalary method.
[00:04:19] Whatever is defined within a certain method is only accessible within that method. No other methods can utilize it. Basically, if I think even broader than that, it is scoped to the curly braces in which it was defined. So even if I had a while loop, if I had an if statement, if I define some variable within the curly braces of that loop, of that decision structure, anything outside of that would not know that variable.
[00:04:52] So it's defined in scope, it's scoped to where it was defined, the curly braces in which it was defined. So if I want to use this scanner in multiple methods inside of this class, I would need to move it outside of the methods and put it at a higher level.
[00:05:12] So the curly braces in which this now is defined is the entire class. That means methods are in that class as well, they now can access it, yes.
>> What does that imply for closing the scanner, like in all these different little methods?
>> Yes, yes, yes, great question.
[00:05:32] Let's make this static, all right, [LAUGH] to the static point, I know I haven't gotten deep into it yet. But even with this outside of the class, this is still complaining, because this method is static, which means it can only access other static things. Unless we did something special, which I'll talk about in a bit.
[00:05:59] All right, so you asked about closing it, that's very good. So, what I would do, I'm gonna use scanner here, let's go ahead, I'll answer that as we get to it. Let's go ahead and enter the code here. So we can say, double salary = scanner.nextDouble, oops, right?
[00:06:27] And this is supposed to return something, so I can say return salary, and then for getCreditScore, I want to ask them for their credit score. And we'll say double creditScore = nextDouble, and we'll return this. Okay, now, for both of these, I am done. Remember, I said I'm using the main, this is a double.
[00:07:11] Okay, I'll make this an int, sorry, you all. And then this must be an int, and then this must be an int. Okay, I'll give you a minute. I updated creditScore to use integers. All right, so remember I said that we're using the main method to control the flow of the program.
[00:07:34] So I know I'm done getting my input right here, I can go ahead and close it right here. Okay?
>> So even if someone were to give you an invalid value for the salary or the credit score, it's not gonna hit that close scanner line until it gets the valid entries.
>> Right, so, if I wanted to put a while loop inside of here to make sure I get the values, then that's fine. And then it wouldn't close it until I'm back here, all right? Okay, so we got the input. Now we want to check if the user is qualified, right?
[00:08:14] So we can say boolean qualified =, and we'll call a new method that we haven't written yet. Remember, I said about the boolean, so you kinda want some sort of questions. So we can say isUserQualified. And we can pass in isUserQualified, we wanna take the salary in the credit score, right?
[00:08:42] So we can say (double salary, and int creditScore). Oops, I'm defining it right here by mistake. Hold on a second. So I would just pass it in salary and creditScore, and then I can define that method. So, little tip, when this is wrong like this red, i can just click here and it'll give me something says, you want me to create a method for you?
[00:09:20] And make it for me nicely like this. It's made this private. That was fine, but i'll make it public until we talk more about private. Okay.
>> How come it's complaining at you, cuz it was expecting a return type. So there it is.
>> That's right, all right.
[00:09:40] So inside of this isUserQualified, we can say, this is decision, right, decision time. So if the credit score, let's start with the salary. If the (salary >= requiredSalary), and where did I get requiredSalary from? I define that at the class level. These are known as global variables. Global because they can be used inside of the entire class.
[00:10:13] They're not local, local is where you will use it only within a method, okay? So if(salary >= requiredSalary &&, so we're using that and, logical operator, creditScore >= requiredCreditScore), then we can return true. The user has met the requirements. Otherwise return false. Notice here, I didn't use curly braces.
[00:10:58] When you have just one statement with an if or an else, you don't have to use curly braces. It's up to you. Some development shops have conventions and rules about this, like don't do single lines. So you could do it like this, you could do it like that, same thing, if it's just one line, okay?
[00:11:23] I'll return out, leave it like that, so you have a couple of examples in the Git repository. All right, so we're returning a value back. Whatever we return back will be stored inside of this boolean qualified variable, all right? And then a final step, let's just go ahead and notify the user.
[00:11:44] So this will be another method. We'll call this notifyUser, and that'll take that qualified variable. And we'll have them create this for us. So this, again, takes a boolean, it won't return anything, and we'll just say this is a if, right? If, Qualified, Then we wanna print something.
[00:12:19] Congrat, you've been approved. Otherwise, Sorry, you've been declined, okay? Now, Let's look at isUserQualified in here. So notice, when I call it, I'm using salary and creditScore, and then here, I also call it salary and creditScore. Again, I just wanna re-emphasize, these are not the same variables. I could call this literally anything.
[00:13:15] Let's call it just s, and call this s, and that will still be perfectly fine. S will correspond to salary there. Does not have to be the same name. And when it is the same name, they are not the same thing, the same memory location, okay? All right.
>> I think when you create your scanner, you're setting it the system in.is in quotes.
>> Is it? Thank you. Perfect, Very nice catch. All right, let's run it without that. All right, let's see, 50,000. Okay, great, that was nice. Okay, credit score, let's say 725. So should I qualify?
[00:14:14] According to this required salary, I should, right? So it says, congrats, I've been approved. All right, let's try again. This time, let's say only 10,000, but let's have the good credit score. So let's say 800, and then sorry, I've been declined. Okay, good. So, yes, question.
>> It's a couple of questions.
[00:14:49] For the class attributes and methods, are those default public?
>> No, they're default protected, meaning anyone inside of the package would be able to access it, any other classes inside of the package, but not outside of the package.
>> Can you not just return the value of evaluation in Java?
>> Yeah, I can. So the question here is, so I did if cuz I wanted you to just see exactly what was going on. I could simplify this a lot by just saying return whatever this equates to. I would probably put this inside of parentheses so it knows that that's one thing.
[00:15:40] And then I can just comment this out and I could simplify the whole thing like that. So again, the variables that were defined at the class level, these are global. Anything else is local, yes?
>> Required salary, should it be double?
>> It can, yeah. I hate to mess with it now.
>> Well, you could just use the refactor intelligent.
>> I don't know if it refactor is the data type. Yeah, it only refactors the name I think. Unless I just don't know how to do it. But okay, where do I use this? Double salary, I think I only use it in this comparison.
[00:16:27] So, let's just run it again to make sure we didn't break it. 50,000, 800, all right, cool.