Chapter 5.8.2 – Inner Classes | Introduction to Programming Using Java

Chapter 5.8.2 – Inner Classes | Introduction to Programming Using Java

 

5.8.2 Inner Classes

 

Chapter 5.8.2 - Inner Classes | Introduction to Programming Using Java

 

Non-static nested classes are referred to as inner classes. Inner classes are not, in practice, very different from static nested classes, but a non-static nested class is actually associated with an object rather than with the class in which its definition is nested. This can take some getting used to.

Any non-static member of a class is not really part of the class itself (although its source code is contained in the class definition). This is true for inner classes, just as it is for any other non-static part of a class. The non-static members of a class specify what will be contained in objects that are created from that class. The same is true—at least logically—for inner classes. It’s as if each object that belongs to the containing class has its own copy of the nested class (although it does not literally contain a copy of the compiled code for the nested class).

This copy has access to all the instance methods and instance variables of the object, even to those that are declared private. The two copies of the inner class in two different objects differ because the instance variables and methods they refer to are in different objects. In fact, the rule for deciding whether a nested class should be static or non-static is simple: If the nested class needs to use any instance variable or instance method from the containing class, make the nested class non-static. Otherwise, it might as well be static.

In most cases, an inner class is used only within the class where it is defined. When that is true, using the inner class is really not much different from using any other class. You can create variables and declare objects using the simple name of the inner class in the usual way.

 

inner classes

 

From outside the containing class, however, an inner class has to be referred to using a name of the form hvariableNamei.hNestedClassNamei, where hvariableNamei is a variable that refers to the object that contains the inner class. In order to create an object that belongs to an inner class, you must first have an object that belongs to the containing class. (When working inside the class, the object “this” is used implicitly.)

Looking at an example will help, and will hopefully convince you that inner classes are really very natural. Consider a class that represents poker games. This class might include a nested class to represent the players of the game. The structure of the PokerGame class could be:

 

Chapter 5.8.2 - Inner Classes | Introduction to Programming Using Java

 

Chapter 5.8.2 - Inner Classes | Introduction to Programming Using Java

 

 

 

 

 

 

 

If game is a variable of type PokerGame, then, conceptually, game contains its own copy of the Player class. In an instance method of a PokerGame object, a new Player object would be created by saying “new Player()”, just as for any other class. (A Player object could be created outside the PokerGame class with an expression such as “game.new Player()”. Again, however, this is rare.) The Player object will have access to the deck and pot instance variables in the PokerGame object.

Each PokerGame object has its own deck and pot and Players. Players of that poker game use the deck and pot for that game; players of another poker game use the other game’s deck and pot. That’s the effect of making the Player class non-static. This is the most natural way for players to behave.

A Player object represents a player of one particular poker game. If Player were an independent class or a static nested class, on the other hand, it would represent the general idea of a poker player, independent of a particular poker game.

 

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 5.2.2 – Constructors | Introduction to Programming Using Java

Chapter 5.5.3 – Example: Vehicles | Introduction to Programming Using Java

Chapter 5.6 – This and Super | Introduction to Programming Using Java

Chapter 5.7 – Interfaces | Introduction to Programming Using Java

Chapter 5.8 – Nested Classes | Introduction to Programming Using Java

1 thought on “Chapter 5.8.2 – Inner Classes | Introduction to Programming Using Java”

Leave a Comment