spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / java / beginning / chap5 / 5 current pageTo page 2To page 3To page 4To page 5To page 6
[next]

Beginning Java 2 SDK 1.4 Edition

Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

Nested Classes

[The following is the conclusion of our series of excerpts from chapter 5 of the Wrox Press title, Beginning Java 2 SDK 1.4 Edition. Source code for the examples discussed can be downloaded at the Wrox Web site (free e-mail registration required).]

All the classes we have defined so far have been separate from each other--each stored away in its own file. Not all classes have to be defined like this. You can put the definition of one class inside the definition of another class. The inside class is called a nested class. A nested class can itself have another class nested inside it, if need be.

When you define a nested class, it is a member of the enclosing class in much the same way as other members. A nested class can have an access attribute just like other class members, and the accessibility from outside the enclosing class is determined by the attributes in the same way.

public class Outside {

  // Nested class
  public class Inside {
    // Details of Inside class...
  }

  // More members of Outside class...
}

Here the class Inside is nested inside the class Outside. The Inside class is declared as a public member of Outside, so it is accessible from outside Outside. Obviously a nested class should have some specific association with the enclosing class. Arbitrarily nesting one class inside another would not be sensible. The enclosing class here is referred to as a top-level class. A top-level class is a class that contains a nested class but is not itself a nested class.

Our nested class here only has meaning in the context of an object of type Outside. This is because Inside is not declared as a static member of the class Outside. Until an object of type Outside has been created, you can't create any Inside objects. However, when you declare an object of a class containing a nested class, no objects of the nested class are necessarily created--unless of course the enclosing class's constructor creates them. For example, suppose we create an object with the following statement:

Outside outer = new Outside();

No objects of the nested class, Inside, are created. If you now wish to create an object of the type of the nested class you must refer to the nested class type using the name of the enclosing class as a qualifier. For instance, having declared an object of type Outside, we can create an object of type Inside as follows:

Outside.Inside inner = outer.new Inside();    // Define a nested class object

Here we have created an object of the nested class type that is associated with the object, outer, created earlier. We are creating an object of type Inside in the context of the object outer. Within non-static methods that are members of Outside, you can use the class name Inside without any qualification as it will be automatically qualified by the compiler with the this variable. So we could create a new Inside object from within the method of the object Outside:

Inside inner = new Inside();    // Define a nested class object

which is equivalent to:

this.Inside inner = this.new Inside();    // Define a nested class object

All this implies that a static method cannot create objects of a non-static nested class type. Because the Inside class is not a static member of the Outside class, such a member could refer to an object which does not exist--an error, if there are no Inside objects extant in the context of an Outside object. And as Inside is not a static data member of the Outside class, if a static method in the Outside class tried to create an object of type Inside directly, without first invoking an object of type Outside, it would be trying to create an object outside of that object's legitimate scope--an illegal maneuver.

Further, because the class Inside is not a static member of the Outside class, it cannot in turn contain any static data members itself. Since Inside is not static, it cannot act as a freestanding class with static members--this would be a logical contradiction.

Nested classes are typically used to define objects that at least have a strong association with objects of the enclosing class type, and often there is a tight coupling between the two. A further use for nested classes is for grouping a set of related classes under the umbrella of an enclosing class. We will be using this approach in examples later on in the book.


home / programming / java / beginning / chap5 / 5 current pageTo page 2To page 3To page 4To page 5To page 6
[next]

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
  Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Fixing MySQL Replication · Firewall Guide: First Steps to Securing the Enterprise · VoxOx Tames the Tumultuous Communications Tangle

Created: August 5, 2002
Revised: August 5, 2002

URL: http://webreference.com/programming/java/beginning/chap5/5/