Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet111/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   107   108   109   110   111   112   113   114   ...   245
Bog'liq
Java 17 Recipes

 How It Works
Sometimes it is important to encapsulate functionality within a single class. It does not 
make sense to include a separate class for functionality that is only used within one other 
class. Imagine that you are developing a GUI, and you need to use a class to support 
functionality for one button. If there is no reusable code within that button class, it does 
not make sense to create a separate class and expose that functionality for other classes 
to use. Instead, it makes sense to encapsulate that class inside the class that requires 
the functionality. This philosophy is a use case for inner classes (also known as nested 
classes).
An inner class is a class that is contained within another class. The inner class can 
be made public, private, or protected like any other class. It can contain the same 
functionality as a normal class; the only difference is that the inner class is contained 
within an enclosing class, otherwise referred to as an outer class. The solution to this 
recipe demonstrates this technique. The TeamInner class contains one inner class 
named Player. The Player class is a JavaBean class that represents a Player object.
Chapter 5 ObjeCt-Oriented java


205
The Player object can inherit functionality from its containing class, including its 
private fields. This is because inner classes contain an implicit reference to the outer 
class. It can also be accessed by containing the TeamInner class, as demonstrated within 
the constructPlayer() method.
public Player constructPlayer(String first, String last, String position, 
int status){
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 example, 
the constructPlayer() method could be called any number of times, instantiating a 
new inner class instance. However, when the outer class is instantiated, no inner class 
instances are instantiated. Similarly, when the outer class is no longer in use, all inner 
class instances are destroyed.
Inner classes can reference outer class methods by referring to the outer class and 
the method(s) it wants to call. The following line of code demonstrates such a reference 
using the same objects 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 
instance variables or methods of its enclosing class. The following is an example of a 
static inner class.
Chapter 5 ObjeCt-Oriented java


206
public class StaticInnerExample {
static String hello = "Hello";
public static void sayHello(){
System.out.println(hello);
}
static class InnerExample {
String goodBye = "Good Bye";
public void sayGoodBye(){
System.out.println(this.goodBye);
}
}
public static void main (String[] args){
StaticInnerExample.sayHello();
StaticInnerExample.InnerExample inner =
new StaticInnerExample.InnerExample();
inner.sayGoodBye();
}
}
Inner classes help to provide encapsulation of logic. Furthermore, they allow 
inheritance of private fields, which is not possible using a standard class.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   107   108   109   110   111   112   113   114   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling