From the course: Java 11+ Essential Training

Declare and use custom classes - Java Tutorial

From the course: Java 11+ Essential Training

Start my 1-month free trial

Declare and use custom classes

- [Instructor] As an application becomes more complex, you'll want to reorganize your code, and put the code into separate files. Generally in Java this means creating additional classes. In a command line application like this, your starting class is typically called main, but then you can create as many other classes as you want. In this demonstration, I'm going to create a class that has a number of methods that can be called from anywhere in the application that execute the different calculations. I'll go on to my project window. I've opened up this package, com.company. You might see this as a tree like this, or if you have compact middle packages turned on, you might see it like this with dot notation. Either one will work. I'll right click on that package, and choose new, Java class. And I'll give my new class a name of calcHelper. You could name it anything you want. Now within the class I'm going to declare four methods. Each of these methods will be marked as static, which means that the methods can be called from the class itself, rather than from an instance of the class. I'll start off with the access modifier public. That means that the method can be called from anywhere in the application. Then I'll say it's static. Then I'll say it's going to return a double value. I'll name the method addValues, and the addValues method will receive two parameters. They'll both be doubles, d1 and d2. Now I have to return a double value. So I'll use return statement, and return d1 plus d2. And that method is complete. Now select those lines of codes, and I'm going to duplicate this code three times. I'm pressing command key on MAC or control d on Windows. Then I'll go back and rename the additional methods. This one will be called subtract values, and it'll do subtraction. This one will be multiply values; it'll do multiplication. And this one will be called divide values. And I'll change that operation as well. And now my calcHelper class is ready to use. I'll go back to the main class. And I'll change all this code here so that instead of explicitly doing the operation, I'll use those static methods. I'll start with the name of the class, calcHelper, then I'll press dot, and I'll choose the method that I want to call, and I'll pass in the values. Make sure you pass them in in the same order in which they're declared. Now I'll copy this bit of code, and I'll paste it here and here and here. I'll come back now to each of these calls and I'll change to the appropriate method. The first one is subtract values, the second one is multiply, and the third is divide. For each of these, I'm just typing the beginning of the method name, and then pressing tab. Now, I've abstracted the functionality. That means I've taken this functionality that's in my top level class, and I've encapsulated the functionality and put it into a separate method in another class. The functionality of my application hasn't changed at all. I can still enter any number, and then I can execute any operation, and I'll get back the result. But now the way I do it is completely hidden in this special class. Again, this is called abstraction. You're taking the functionality that you have in your top level of the application, and you're hiding it in another part of the application. This lets you organize your code so that it's more maintainable over time, but also creates simple methods that can be called from everywhere without having to know exactly how they work on the inside.

Contents