From the course: Java 8 Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Creating nested and anonymous classes

Creating nested and anonymous classes - Java Tutorial

From the course: Java 8 Essential Training

Start my 1-month free trial

Creating nested and anonymous classes

- I've previously described how to create custom classes that are libraries of methods that you can call from anywhere. For example, this MathHelper class is defined in a file named MathHelper.java and it's public. And all of its methods are public. But you can also define classes that are nested within other classes, and those classes are only available to their parent classes. I'm going to start by refactoring or modifying this code. I'm going to take all the code within the Main method and extract it to its own separate method. And I'm going to turn this class file into something I can create an instance of. I'll select all that code within the braces then I'll go to the menu and choose refactor, extract, method. I'll name my new method calculate, and I'll change the access modifier to protect it. That means that this class can be used by any other class in the same package. I'll click okay and that creates the new method, but it also adds code to call the method from within the…

Contents