Lecture Fundamentals of computing 1: Lecture 14 (cont) introduce inner class. In this chapter students will be able to understand: What is inner class? What is static inner class? And anonymous inner class. Inviting you refer. | Lecture Title: Inner class Fundamentals of Computing 1 Agenda Inner class Static inner class Anonymous Inner Class ask them, how might the computer store "hi" using binary digits? (some kind of mapping; ASCII) An inner class is a class declared inside another class Basic structure for creating an inner class: class outerClassName { private class innerClassName { // body of inner class } } Inner class Visibility of inner class public, protected, or private These visibilities determine whether other classes can see the inner class. Understanding inner classes An inner class automatically has access to all the fields and methods of the outer class An inner class carries with it a reference to the current instance of the outer class that enables it to access instance data of the outer class. Understanding inner classes (con’t) Because of the outer class instance reference, you can’t create or refer to an inner class from a static method of the outer class. One of the main reasons for creating an inner class is to create a class that’s only of interest to the outer class. As a result, you usually declare inner classes to be private so other classes can’t access them. Understanding inner classes (con’t) Occasionally, code in an inner class needs to refer to the instance of its outer class. To do that, you list the name of the outer class followed by the dot operator and this. Example: if the outer class is named MyOuterClass, you would use inside the inner class to refer to the instance of the outer class. Agenda Inner class Static inner class Anonymous Inner Class ask them, how might the computer store "hi" using binary digits? (some kind of mapping; ASCII) Static inner class A static inner class is similar to an inner class, but doesn’t require an instance of the outer class. Basic form is the following: class outerClassName { private static class innerClassName { // body of inner class } Static inner class (cont’d) Like a static method, a static inner class can’t access any non-static fields ormethods in its outer class. It can, however, access static fields or methods. Agenda Inner class Static inner class Anonymous inner Class ask them, how might the computer store "hi" using binary digits? (some kind of mapping; ASCII) Anonymous Inner Class Anonymous inner classes (anonymous classes) are probably the strangest feature of the Java programming language. An anonymous class is a class that’s defined on the spot, right at the point where you want to instantiate it. Because you code the body of the class right where you need it, you don’t have to give it a name. That’s why it’s called an anonymous class. Creating an anonymous class The basic form for declaring and instantiating an anonymous class is this: new ClassOrInterface() { class-body } Example of anonymous class Understanding Anonymous Class