Join David Gassner for an in-depth discussion in this video Creating and instantiating custom classes, part of Java Essential Training.
- View Offline
- In this project, Create Class, I'm going to demonstrate breaking code out to a separate class. I'm working with a version of my Calculator application. It allows the user to type in two numeric values and select a math operation. The code to collect data is in the getInput method. It's called three times. The code to do all the math operations is down here in four separate methods. Now, let's take a scenario where I need to break out these methods to a separate class to make it easier to call them from multiple places in the application.
My first step will be to create a new class. I could put the class in the same package as before, com.example.java, but one very common practice is to create subpackages for special parts of the application. I'll start by creating a new package. I'll right click on the base package, com.example.java, in the Project window, and I'll choose New, Package. I'll enter a new package name of utilities and click OK.
That creates a secondary package. Now, if I right click on that and choose Show in Explorer, or Show in Finder on Mac, I'll see that this utilities package is now a subpackage of com.example.java. That's where I'll create my new class. Now, I'll right click on the new package and choose New, Java Class. I'll name my class MathHelper and click OK.
I'll get rid of this automatically-generated comment and then make a little bit of space inside the class declaration. Now, I'll return to my main class, Calculator.java. I'll select my four methods that are doing the math, I'll cut them to the clipboard, I'll go to MathHelper and I'll paste. Notice that the name of the class and the names of the methods are currently dimmed out. That's a visual indicator that these are not being used in the application yet. So far, so good.
Now, I'll come back to Calculator.java, get rid of a little bit of extra white space, and then I'll come back up here to my call to addValues. My goal is to be able to call addValues from the MathHelper class. I'll type in MathHelper and a period and I see immediately that MathHelper can't be found. So I'll hold down the alt or opt key and press enter. That adds an import statement at the top of my code for my MathHelper class.
Just like the classes in java.util, I have to import this class because it's not a part of the same package that my main class is in. But I still have a problem. Now, I'm being told that addValues, that's the method I'm trying to call, is marked with private access in the MathHelper class. The code I copied from my main class has the keyword private on all four methods. That means the methods can only be called from within this class.
That's definitely not what I want. So, I'll go through all four and change private to public. You can do it manually just by retyping or you can place the cursor on the line and press alt or opt and then enter and choose Make 'public'. That's a little bit faster so I'll just go through and do that for all the rest of the methods. Now this class and its methods can be used, Notice, immediately, I'm shown that addValues is now being used, and if I come back to Calculator, I see that the error on that call is gone.
Now, I'll select the name of the class, MathHelper and the dot, and I'll copy and paste that into place on all of the other method calls. I'll then go back to my Project window and look at my Problems view. It tells me "Nothing to show." That means all of my syntax errors have been taken care of and I'm ready to test again. I'll run the application, and it's compiled and ready to run. I'll type a number of 5 and a number of 4 and a - for subtraction.
I get back a correct answer of 1. My main class is now much shorter and easier to navigate. My MathHelper class is very easy to use and I can now use it from anywhere in the application. That's the first level of encapsulation, breaking methods out into their own libraries so that the code can be called from anywhere.
- Understanding the history and principles of Java
- Installing Java, IntelliJ IDEA, and BlueJ
- Creating a Java project
- Working with variables, values, and expressions
- Working with object data types
- Building, comparing, and parsing strings
- Debugging and exception handling
- Creating loops and reusable code
- Passing arguments by reference or value
- Using simple and complex arrays
- Creating custom classes
- Understanding inheritance and polymorphism
- Managing files with Java libraries
- Documenting code with Javadoc
- Packaging classes in JAR files