You are previewing Java 7: A Beginner's Tutorial (Third Edition).

Java 7: A Beginner's Tutorial (Third Edition)

Cover of Java 7: A Beginner's Tutorial (Third Edition) by Budi Kurniawan Published by Brainy Software
  1. Cover Page
  2. Title Page
  3. Copyright Page
  4. Table of Contents
  5. Introduction
    1. Java, the Language and the Technology
    2. An Overview of Object-Oriented Programming
    3. About This Book
    4. Downloading and Installing Java
    5. Downloading Program Examples and Answers
  6. Chapter 1: Your First Taste of Java
    1. Your First Java Program
    2. Java Code Conventions
    3. Integrated Development Environments (IDEs)
    4. Summary
    5. Questions
  7. Chapter 2: Language Fundamentals
    1. ASCII and Unicode
    2. Separators
    3. Primitives
    4. Variables
    5. Constants
    6. Literals
    7. Primitive Conversions
    8. Operators
    9. Comments
    10. Summary
    11. Questions
  8. Chapter 3: Statements
    1. An Overview of Java Statements
    2. The if Statement
    3. The while Statement
    4. The do-while Statement
    5. The for Statement
    6. The break Statement
    7. The continue Statement
    8. The switch Statement
    9. Summary
    10. Questions
  9. Chapter 4: Objects and Classes
    1. What Is a Java Object?
    2. Java Classes
    3. Creating Objects
    4. The null Keyword
    5. Objects in Memory
    6. Java Packages
    7. Encapsulation and Access Control
    8. The this Keyword
    9. Using Other Classes
    10. Final Variables
    11. Static Members
    12. Static Final Variables
    13. Static import
    14. Variable Scope
    15. Method Overloading
    16. By Value or By Reference?
    17. Loading, Linking, and Initialization
    18. Object Creation Initialization
    19. Comparing Objects
    20. The Garbage Collector
    21. Summary
    22. Questions
  10. Chapter 5: Core Classes
    1. java.lang.Object
    2. java.lang.String
    3. java.lang.StringBuffer and java.lang.StringBuilder
    4. Primitive Wrappers
    5. Arrays
    6. java.lang.Class
    7. java.lang.System
    8. java.util.Scanner
    9. Boxing and Unboxing
    10. Varargs
    11. The format and printf Methods
    12. Summary
    13. Questions
  11. Chapter 6: Inheritance
    1. An Overview of Inheritance
    2. Accessibility
    3. Method Overriding
    4. Calling the Superclass's Constructors
    5. Calling the Superclass's Hidden Members
    6. Type Casting
    7. Final Classes
    8. The instanceof Keyword
    9. Summary
    10. Questions
  12. Chapter 7: Error Handling
    1. Catching Exceptions
    2. try without catch
    3. Catching Multiple Exceptions
    4. The try-with-resources Statement
    5. The java.lang.Exception Class
    6. Throwing an Exception from a Method
    7. User-Defined Exceptions
    8. Final Words on Exception Handling
    9. Summary
    10. Question
  13. Chapter 8: Numbers and Dates
    1. Number Parsing
    2. Number Formatting
    3. Number Parsing with java.text.NumberFormat
    4. The java.lang.Math Class
    5. The java.util.Date Class
    6. The java.util.Calendar Class
    7. Date Parsing and Formatting with DateFormat
    8. Summary
    9. Questions
  14. Chapter 9: Interfaces and Abstract Classes
    1. The Concept of Interface
    2. The Interface, Technically Speaking
    3. Base Classes
    4. Abstract Classes
    5. Summary
    6. Questions
  15. Chapter 10: Enums
    1. An Overview of Enum
    2. Enums in a Class
    3. The java.lang.Enum Class
    4. Iterating Enumerated Values
    5. Switching on Enum
    6. Summary
    7. Questions
  16. Chapter 11: The Collections Framework
    1. An Overview of the Collections Framework
    2. The Collection Interface
    3. List and ArrayList
    4. Iterating Over a Collection with Iterator and for
    5. Set and HashSet
    6. Queue and LinkedList
    7. Collection Conversion
    8. Map and HashMap
    9. Making Objects Comparable and Sortable
    10. Summary
    11. Questions
  17. Chapter 12: Generics
    1. Life without Generics
    2. Introducing Generic Types
    3. Using Generic Types without Type Parameters
    4. Using the ? Wildcard
    5. Using Bounded Wildcards in Methods
    6. Writing Generic Types
    7. Summary
    8. Questions
  18. Chapter 13: Input/Output
    1. File Systems and Paths
    2. File and Directory Handling and Manipulation
    3. Input/Output Streams
    4. Reading Binary Data
    5. Writing Binary Data
    6. Writing Text (Characters)
    7. Reading Text (Characters)
    8. Logging with PrintStream
    9. Random Access Files
    10. Object Serialization
    11. Summary
    12. Questions
  19. Chapter 14: Nested and Inner Classes
    1. An Overview of Nested Classes
    2. Static Nested Classes
    3. Member Inner Classes
    4. Local Inner Classes
    5. Anonymous Inner Classes
    6. Behind Nested and Inner Classes
    7. Summary
    8. Questions
  20. Chapter 15: Swing Basics
    1. AWT Components
    2. Useful AWT Classes
    3. Basic Swing Components
    4. Summary
    5. Questions
  21. Chapter 16: Swinging Higher
    1. Layout Managers
    2. Event Handling
    3. Working with Menus
    4. The Look and Feel
    5. Fast Splash Screens
    6. System Tray Support
    7. Desktop Help Applications
    8. Summary
    9. Questions
  22. Chapter 17: Polymorphism
    1. Defining Polymorphism
    2. Polymorphism in Action
    3. Polymorphism in a Drawing Application
    4. Polymorphism and Reflection
    5. Summary
    6. Questions
  23. Chapter 18: Annotations
    1. An Overview of Annotations
    2. Standard Annotations
    3. Common Annotations
    4. Standard Meta-Annotations
    5. Custom Annotation Types
    6. Summary
    7. Questions
  24. Chapter 19: Internationalization
    1. Locales
    2. Internationalizing Applications
    3. An Internationalized Swing Application
    4. Summary
    5. Questions
  25. Chapter 20: Applets
    1. A Brief History of Applets
    2. The Applet API
    3. Security Restrictions
    4. Writing and Deploying Applets
    5. How AppletViewer Works
    6. Passing Parameters to an Applet
    7. SoundPlayerApplet
    8. JApplet
    9. Applet Deployment in a JAR File
    10. Faster Loading
    11. Summary
    12. Questions
  26. Chapter 21: Java Networking
    1. An Overview of Networking
    2. The Hypertext Transfer Protocol (HTTP)
    3. java.net.URL
    4. java.net.URLConnection
    5. java.net.Socket
    6. java.net.ServerSocket
    7. A Web Server Application
    8. Summary
    9. Questions
  27. Chapter 22: Java Database Connectivity
    1. Introduction to JDBC
    2. Four Steps to Data Access
    3. Closing JDBC Objects
    4. Reading Metadata
    5. The SQLTool Example
    6. Summary
    7. Questions
  28. Chapter 23: Java Threads
    1. Introduction to Java Threads
    2. Creating a Thread
    3. Working with Multiple Threads
    4. Thread Priority
    5. Stopping a Thread
    6. Synchronization
    7. Visibility
    8. Thread Coordination
    9. Using Timers
    10. Swing Timers
    11. Summary
    12. Questions
  29. Chapter 24: Concurrency Utilities
    1. Atomic Variables
    2. Executor and ExecutorService
    3. Callable and Future
    4. Swing Worker
    5. Locks
    6. Summary
    7. Questions
  30. Chapter 25: Security
    1. Java Security Overview
    2. Using the Security Manager
    3. Policy Files
    4. Permissions
    5. Using the Policy Tool
    6. Applet Security
    7. Programming with Security
    8. Cryptography Overview
    9. Creating Certificates
    10. The KeyTool Program
    11. The JarSigner Tool
    12. Java Cryptography API
    13. Summary
    14. Questions
  31. Chapter 26: Java Web Applications
    1. Servlet Application Architecture
    2. Servlet API Overview
    3. Servlet
    4. Writing a Basic Servlet Application
    5. ServletRequest
    6. ServletResponse
    7. ServletConfig
    8. ServletContext
    9. GenericServlet
    10. HTTP Servlets
    11. Using the Deployment Descriptor
    12. Summary
    13. Questions
  32. Chapter 27: JavaServer Pages
    1. A JSP Overview
    2. jspInit, jspDestroy, and Other Methods
    3. Implicit Objects
    4. JSP Syntactic Elements
    5. Handling Errors
    6. Summary
    7. Questions
  33. Chapter 28: Javadoc
    1. Writing Documentation in Java Classes
    2. Javadoc Syntax
    3. Summary
    4. Question
  34. Chapter 29: Application Deployment
    1. JWS Overview
    2. JNLP File Syntax
    3. A Deployment Example
    4. Security Concerns
    5. Summary
    6. Questions
  35. Appendix A: javac
    1. Options
    2. Command Line Argument Files
  36. Appendix B: java
    1. Options
  37. Appendix C: jar
    1. Syntax
    2. Options
    3. Examples
    4. Setting an Application's Entry Point
  38. Appendix D: NetBeans
    1. Download and Installation
    2. Creating a Project
    3. Creating a Class
    4. Running a Java Class
    5. Adding Libraries
    6. Debugging Code
  39. Appendix E: Eclipse
    1. Download and Installation
    2. Creating a Project
    3. Creating a Class
    4. Running a Java Class
    5. Adding Libraries
    6. Debugging Code
  40. Index
O'Reilly logo

Chapter 14

Nested and Inner Classes

Nested and inner classes are often considered too confusing for beginners. However, they have some merits that make them a proper discussion topic in this book. To name a few, you can hide an implementation completely using a nested class, and it provides a shorter way of writing an event-listener.

One of the reasons nested and inner classes sometimes seem overly complex is because the terms “nested classes” and “inner classes” are often used to mean different things in different texts. This book will stick to the definitions in The Java Language Specification, Third Edition, which is the formal specification from Sun Microsystems (http://java.sun.com/docs/books/jls/).

This chapter starts by defining what ...

The best content for your career. Discover unlimited learning on demand for around $1/day.