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 - Java Tutorial
From the course: Java 8 Essential Training
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
-
-
-
(Locked)
Understanding encapsulation7m 19s
-
Creating and instantiating custom classes4m 51s
-
(Locked)
Creating nested and anonymous classes4m 17s
-
(Locked)
Visualizing class relationships3m 43s
-
(Locked)
Storing data in instance variables6m 2s
-
(Locked)
Declaring instance and static methods3m 59s
-
(Locked)
Using constructor methods4m 56s
-
(Locked)
Using static variables as constants3m 3s
-
(Locked)
Declaring and using enum types5m 17s
-
(Locked)
-
-
-
-