magazine resources subscribe about advertising

New Architect Daily
Commentary and updates on current events and technologies

CMP Media E-Book

Download your copy today.

Research
Search for reports and white papers from industry vendors and analysts.

This Week at NewArchitect.com Subscribe now to our free email newsletter and get notified when the site is updated with new articles







Day of Defeat Online Gaming

 New Architect > Archives > 1997 > 11 > Java Alley  

Inner Classes in Depth

By Bruce Eckel

In Java 1.1, it's possible to place a class definition within another class definition, creating an "inner class" that allows you to group classes that logically belong together and to control the visibility of one within the other. However, it's important to understand that inner classes are distinctly different from composition.

You create an inner class by placing the class definition inside a surrounding class, as in Listing One. This example hides the class definitions for Contents and Destination inside Parcel1 but note that it doesn't make them handles or objects; those must be created explicitly. This is done in main(), and it reveals a somewhat odd syntax. Creating an object of the outer class follows the typical form: Parcel1 p=new Parcel1();

An object of the inner class would be of type OuterClassName.InnerClassName, but to create the object you don't refer to the outer class name Parcel1; instead, you must use an object of the outer class to make an object of the inner class, like so: Parcel1.Contents c=p.new Contents().

Without an object of the outer class, you can't create an object of the inner class because every inner-class object is quietly connected to the outer-class object from which it was created.

Usually, an outer-class method will return a handle to an inner class, as in



  Day of Defeat Online Gaming

home | daily | current issue | archives | features | critical decisions | case studies | expert opinion | reviews | access | industry events | newsletter | research | careers | info centers | advertising | subscribe | subscriber service | editorial calendar | press | contacts


Copyright © 2006 CMP Media, LLC Read our privacy policy, your California privacy rights, terms of service.
SDMG Web sites: BYTE.com, C/C++ Users Journal, Developer Pipeline, Dr. Dobb's Journal, DotNetJunkies, MSDN Magazine, Sys Admin,
SD Expo, SD Magazine, SqlJunkies, The Perl Journal, Unixreview, Windows Developer Network, New Architect

web2