Java Reference
In-Depth Information
Player player = new Player();
player.firstName = first;
player.lastName = last;
player.position = position;
player.status = status;
return player;
}
Outer classes can instantiate an inner class as many times as needed. In the ex-
ample, the constructPlayer() method could be called any number of times, in-
stantiating a new instance of the inner class. However, when the outer class is instanti-
ated, no instances of the inner class are instantiated.
Inner classes can reference outer class methods by referring to the outer class and
then referring to the method that it wants to call. The following line of code demon-
strates such a reference using the same objects that are represented in the solution to
this recipe. Suppose that the Player class needed to obtain the player list from the
outer class; you would write something similar to the following:
TeamInner.this.getPlayerList();
Although not very often used, classes other than the outside class can obtain access
to a public inner class by using the following syntax:
TeamInner outerClass = new TeamInner();
outerClass.player = outerClass.new Player();
Static inner classes are a bit different, in that they cannot directly reference any in-
stance variables or methods of its enclosing class. The following is an example of a
static inner class.
public class StaticInnerExample {
static String hello = "Hello";
public static void sayHello(){
System.out.println(hello);
}
static class InnerExample {
Search WWH ::




Custom Search