Java Lesson 12 | More on Variables
Hey, this is Jake with Mac Heads One on One. This is your 12th Java tutorial, and today I'm going to be talking a little more about variables. I'm going to be talking about something that goes before the data type, so we get a more specific kind of thing to be working with.
So, I'm going to be talking about four of these sort of modifiers, which are static, final, public, and private, and I'm just going to be explaining what they are. So this is probably going to be kind of brief; I'm not going to be making programs or anything like that with them, but I'm going to be saying what they are because you're going to need to know that.
So the first time to talk about is static. So, um, I can't really make a static variable in this main method; I have to make it out here. So, since it goes before the data type, static double x equals 0.
What static means is that it's available; you can use this double x in any method anywhere in this class. Um, yeah, because it's not always the case, and if you run into a situation where you need to be using this x, um, you know, somewhere in the class, and it's not letting you do that, um, making it static will probably fix that because it can be used anywhere in the class.
The next is final, which means that's the value; that's it. You cannot reassign a final variable. If I do x equals 1, I'm getting an error because that's a final variable. That's the final; that's it. You cannot reassign a final variable. Once you assign a final variable, it has that value permanently, so that's what final means.
Then there's public and private. Public means the variable is available to all classes. So, if I do a public double, it means this double is available to all classes: public double x equals zero.
And so what I have to do is I create a second object: second s equals new second, and I can actually print out the variable from the other class; just sx to get that x variable because it's public; it's available to all classes. And I'm just going to run this to prove it—there we go, 0.0.
If I made this private, it means it's only available to this class. The other classes cannot use that variable. Private double... I'm getting an error because I can't do a set x; I can't get that in this class because it's private.
So that's just really brief: public, private, static, final. Um, I'm sorry; I probably didn't go into as much detail as I needed to, or, um, but it's pretty self-explanatory, I think, and you just needed to know all that stuff meant for some later tutorials. So, see you!