In Java, it is also possible to nest classes (a class within a class). The purpose of nested classes is to group classes that belong together, which makes your code more readable and maintainable.
package anote;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
Gearbox mcLaren = new Gearbox(6);
Gearbox.Gear first = mcLaren.new Gear(1, 12.3);
System.out.println(first.driveSpeed(1000));
}
}
class Gearbox {
private ArrayList<Gear> gears;
private int maxGears;
private int gearNumber = 0;
public Gearbox(int maxGears) {
this.maxGears = maxGears;
this.gears = new ArrayList<>();
Gear neutral = new Gear(0, 0.0);
this.gears.add(neutral);
}
public class Gear {
private int gearNumber;
private double ratio;
public Gear (int gearNumber, double ratio){
this.gearNumber = gearNumber;
this.ratio = ratio;
}
public double driveSpeed(int revs){
return revs * ( this.ratio);
}
}
}
12300.0