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

Java Lesson 8


3m read
·Nov 3, 2024

Hey guys, this is Mac. Heads in the one with our last Java lesson.

So, as you may know, after this Java lesson, we're going to move into Mac programming. But anyway, I'm going to do our last Java lesson once and for all. In this Java lesson, I'm going to be teaching you classes and how to call methods on other classes, how to declare classes, how to make classes, etc.

I'm going to open up Xcode. I'm going to create a new project. I'm going to make it Java, you have a tool, and I'm going to call it Classy App. I'll save it to my desktop. So right here, you know, you've been editing Classy App or whatever the name of it is, dot Java. Inside of that is public class Classy App.

What you may not realize is you can make other classes as well. If I right-click on this Classy App class and say new file, and I go to pure Java, Java class, I will call it PrintOutFunctions. Then I can put a public static void on this, just say printName, and then I'll say string name. All this will really do is print line "Hello, name," and that's all it'll actually really do.

But if you take a look at this, how do we call that function on that other class? Well, the answer is simple. Now that you have another class here, and as you can see, it is called PrintOutFunctions, we can just use it like it's a variable. We say PrintOutFunctions space, I'll call pof space, and now you need to make it new PrintOutFunctions, left parenthesis, right parenthesis, semicolon to make a new one.

Now, I'll say pof dot and then whatever we call the function. I think it's printName, and I'll just print the name "Mac Head's a little one." And now I'll get rid of this because we don't really need this. But now watch—hello, Mac Kids, everyone!

So as you can see, we can organize our code. So we can have a class that all it does is do random print things. We can make it so, instead of needing to do system.out.printline, I'll make my own thing in here called printf that'll just run system that have that print. So this way, now if I want to print something, say pof dot print, they'll say "Hi," and I have to manually type backslash n to do a new line because I'm not using print line, I'm just using print. So I'm printing "Hi" right there.

So in this lesson, I've just taught you how to use classes. So I can also put other functions on this class. And here's another cool thing you may know about private functions. Here’s what this private function is: a private function is something that can't be accessed from the outline outside.

So here, I can just do printfp right here. And the interesting thing is that right here, I have this void called printfp, but I will not be able to see that from here. So if I do pof dot printf, it will show up and work; but if I do print fp "Hi," it will not work properly. See? It has private, so you cannot access it from another class.

That goes the same for variables. If I make it public, it's x in a public into y. What I can do from here is I can say pof.x equals maybe 10, pof dot y equals 10. And it'll work perfectly. And of course, we're not printing anything out or anything. But if I make these variables private, then it will not work.

Now another thing I'm going to show you is how to use this more efficiently. So basically, a variable or a class can have methods or functions on it as well as variables. So here's an x and a y. What I'm going to do, I'm going to only have one pof, but it's going to be storing me 2 inches. So that's a way to like save yourself code from declaring two inks.

Um, this way, I can just do pof dot print y plus y, x plus x, and then I'll do another next session. The way this will work is it'll print out 10, 10, and I'll make this 12 or 14 or whatever. Oh, sorry, I forgot to do pof.xml, my fault, totally sorry. So there we go, and it says, "So be ready for Objective C next. Goodbye!"

More Articles

View All
Variance of sum and difference of random variables | Random variables | AP Statistics | Khan Academy
So we’ve defined two random variables here. The first random variable, X, is the weight of the cereal in a random box of our favorite cereal, Matthews. We know a few other things about it. We know what the expected value of X is; it is equal to 16 ounces.…
Tips From an Ultramarathoner for Common Trail Injuries | Get Out: A Guide to Adventure
I’m Scott Jurek, and I’m an ultra-marathoner. Today, we’re gonna be talking about injury prevention. I love pushing myself to the edge, and when I’m injured, I’m not able to train; I’m not able to race. Thus, avoiding injuries — but then once they do occu…
The Stoic Guide To Overcoming The Desire To Escape Everything | STOICISM INSIGHTS
Isn’t it a bit strange that in this vast world we often stick to the same small corners where we were born? Here we are, on this huge spinning globe, and many of us never venture far from where our journey began. Think about it: how often do we find ourse…
The Video Chat That Existed In The 1870s | How Sci-fi Inspired Science
You hear your phone. You look down, and what do you see? Incoming video call. After you hit the client, think about how commonplace video chats have become. For a long time, the idea of seeing someone from across the world was only in science fiction. So,…
How Would Warren Buffett Invest a Small Sum of Money?
I’m Michael Zenger from Danvers, Massachusetts. That’s the town who’s missed, who’s banned Mr. Buffett so generously sent to the Rose Bowl Parade last year. So, you’re a very popular guy in my town. Good morning, Mr. Buffett. Mr. Munger. Mr. Buffett, I w…
Making Physical Retail as Easy as Opening an Online Store - Ali Kriegsman and Alana Branston
So there were a bunch of questions about you guys, kind of like pre-YC. I think maybe the easiest way to do this is to flow through from there. Before you guys were in YC and then fellowship and then Corps, and then now. So going all the way back, Phil Th…