O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

There's more...

An inner class is a non-static nested class. Java also allows you to create a static nested class that can be used when an inner class does not require access to non-public properties and methods of the enclosing class. Here is an example (the keyword static is added to the Engine class):

public class Vehicle {  private Engine engine;  public Vehicle(int weightPounds, int horsePower) {    this.engine = new Engine(horsePower, weightPounds);  }  public double getSpeedMph(double timeSec){    return this.engine.getSpeedMph(timeSec);  }  private static class Engine {    private int horsePower;    private int weightPounds;    private Engine(int horsePower, int weightPounds) {      this.horsePower = horsePower;    } private double getSpeedMph(double timeSec){ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required