June 2012
Intermediate to advanced
288 pages
6h 48m
English
Inner Class
An inner class is a class declared within another class. An inner class is available only from within the class it is declared.
No special syntax is required to declare an inner class. Just declare the inner class within the body of another class. Here’s an example:
public class DiceGame
{
public static void main(String[] args)
{
Dice d = new Dice();
d.roll();
}
class Dice
{
public void roll()
{
// code that rolls the dice goes here
}
}
}
In this example, the Dice class is an inner class defined within the DiceGame class. As a result, the Main method within the DiceGame class can create and use a variable of type Dice.
Read now
Unlock full access