yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Java Lesson 12 | More on Variables


2m read
·Nov 3, 2024

Processing might take a few minutes. Refresh later.

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!

More Articles

View All
Explorer Albert Lin explores a cave burial site filled with ancient carvings
So little is known about the Picts. Searching for their lost kingdom means I must follow every lead, and there’s something on the walls of this cave that’s drawing me in. I’m going to start the scan. Okay, yeah, my handheld Light Art technology allows me…
How to make a smoother entrance into any room or conversation | Melanie Katzman | Big Think
And ever stood at the edge of the ocean and watched how the waves come in and come out, there’s a rhythm. If you enter the ocean at the right time, you catch the wave; you bob up and down in the water. It’s a fabulous experience. If you miss the rhythm, …
Death of the Firstborn: The Final Plague | Biblical Series: Exodus
Why is it the Passover that becomes the memorial in the foundation, and is it the sacrifice of the innocent? And it’s this because it’s the sacrifice of the Lambs and the blood. And so is the idea here that the foundation of the proper state is the volunt…
Coconut Crabs Devour Pig Carcass | Searching For Amelia
[music playing] NARRATOR: If Amelia did survive on this island, there would be no avoiding this formidable creature. Up to three feet across and weighing in at over nine pounds, coconut crabs are the largest on earth, and many thousands of them teem over…
One Type of Job That AI, Robots, and Machines Can't Actually Automate | Andrew McAfee | Big Think
My coauthor Erik Brynjolfsson and I are both at MIT, and we have a colleague at MIT who said something that really helps me understand some of the last human work that I think is ever going to be automated by even really sophisticated technology. Our coll…
Youth brain: How does your office view aging? | Dave Asprey | Big Think
A lot of people think that taking care of your skin or just wanting to look good is about either sexual attractiveness or it’s about ego. It’s sometimes those things. But quite often, it’s something different. It has to do with wanting to be able to take …