Java Lesson 16 | Final and Static Methods
Hey, this is Jake with Maads 101. It's going to be another Java tutorial.
Today, I'm going to talk about two more different kinds of methods. I talked about abstract methods in the last tutorial, and this is going to be relatively brief, but I'm going to talk about static and final methods.
I already talked about static and final variables, so if you remember, a final variable could not be reassigned, and a final method cannot be overwritten. That's final! This is the method you can't override. To do that, you would just put final before it, like final public void print()
, no parameters, and all right! That method could not be overwritten. I'm not going to go through a whole, you know, making a superclass and this and that to prove it, but just know it can't be overwritten.
The next one I'm going to talk about is a static method. So, let's say I had a method in this class. This is going to be an example of it just in this main class, and I had, like, and it was outside the main method: public void print()
. I tried to call print, and I'm getting an error here because I need an object to call the method.
I could do this: MacHeads m = new MacHeads();
and I would be creating an object of this class, the MacHeads type, and then do m.print()
. Or I can do something much simpler, which is make it static. So, public static void print()
—and public static means I do not need an object to call this method.
So, if I just print it out, "Hello," and that's why it makes a lot of sense if this is static. But public static void print()
, this public static void main()
, and then I can just do print()
—no errors because it's static.
I believe you can also put static before public; it doesn't matter. But yeah, so I'll just do public static
, but you can put it before, so public static void print()
, and I don't need an object to call this method because static methods don't need objects to be called. And there it goes! It prints "Hello."
So, I just want to talk about what's coming up next. In the next couple of tutorials—or yeah, I'm going to be talking about GUI, graphical user interface, like, you know, windows and buttons and graphics and stuff like that.
I didn't cover all of the Java basics, but I covered a lot of them, and what you'll need to know to start making GUI. The only reason I actually started making these tutorials on the basics was so that I could eventually start making Java GUI tutorials.
So, the next tutorial is going to be the first GUI tutorial, and I'm just going to be talking about creating a window and a few different ways to do that.
So, see you next time!