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

Java Lesson 8 | Intro to using multiple classes


2m read
·Nov 3, 2024

Processing might take a few minutes. Refresh later.

Hey, this is Jake with Mac Heads 101. It's going to be another Java tutorial, and today I'm going to be giving you an intro to using multiple classes. I'm going to be making a couple more tutorials on this, but it's just going to be an introduction.

So, the first thing we have to do, um, obviously, is actually create a new class. So, file, new class, and that's source folders, the projects, which was our project. A name, and I'll just call it "second" because I don't really need to call it anything else. All right, so this is our second class.

What I'm going to do is I'm going to build a method in here, right? And then I'm going to be able to call that method in this class, and I'm going to show you how. We're not building a main method in here because Java actually runs from this main method. In this main method, we're going to be calling a method from this class.

So, I'm just going to make a very simple method, public void, and I'm going to explain what all this means. Print, and I'm going to call it print. All right, public means it is a public method, which means I can use it in this class. All the other classes have access to it. If I made it private, I would not be able to call the method in this class.

Void means it does not return a value, as in if I had something like, um, public double, I would have to make it return a double. So, like, return 18, and then I could, when I called that method, I could set a variable equal to that, um, because it was returning 18. But void means it's not going to be returning anything because all we're going to do is printing.

And I got a little ahead of myself there, so I apologize. But, um, system.out.printLn, and in here, I'm just gonna be writing "Hello, World." So I can't make anything better to write.

All right, so we have a method called print. So the first thing we have to do here is create an object for the second class. So, seconds, and I'll call s equals new second, and empty parameters.

Okay, so now we have a method, an object, and in order to call, um, a method, we do that from that class. We do that object dot, and our method name was print, and it took no parameters.

And so if I run it, it calls that print method, and it prints "Hello, World." All right, and so that is just a really quick introduction to, um, using multiple classes.

In the next tutorial, I'm going to be getting a little more in depth. I'm going to be talking about methods that return values, methods that take parameters, possibly setters, getters, and constructors if I have time. So I'll see you the...

More Articles

View All
What If Everyone JUMPED At Once?
Hey, Vsauce. Michael here. And what if every single person on Earth jumped at the exact same time? Could it cause an earthquake or would we not even be able to tell? Well, first things first, let’s talk about the Earth’s rotation. The Earth spins, that’s …
Comparing European and Native American cultures | US history | Khan Academy
In the first years of interaction between Native Americans and Europeans, there were a lot of aspects of each other’s cultures that each group found, well, just plain weird. Europeans and Native Americans looked, dressed, and thought differently in fundam…
I Just Lost $1.5 Million In Stocks
What’s up guys, it’s Graham here. So let’s be real, everyone always talks about their wins or how they knew and predicted that some obscure event was going to happen in the future. But in a market like this, I think it’s really important that we talk abou…
Torque Basics | Simple harmonic motion and rotational motion | AP Physics 1 | Khan Academy
Imagine you’ve got a door here with a blue doorknob. Any one of these 10-newton forces will cause the door to rotate around the hinge, or the axis, or sometimes this is called the pivot point. Any one of these forces will cause the door to rotate. My que…
Is Our Food Becoming Less Nutritious?
Is our food becoming less nutritious? Many people claim that the nutrient content in our food has been decreasing over the decades. But is this really true, and should we be worried? A study published in 2004 looked at 43 different common garden crops, a…
The Key To A Long-term Relationship
How do you know when you meet your perfect partner that you think is going to be with you for life? Maybe you get married, maybe you don’t. But my point is, why are they the right one for you? You have to think long term. Are they your partner because th…