Skip to content

Latest commit

 

History

History
33 lines (28 loc) · 727 Bytes

File metadata and controls

33 lines (28 loc) · 727 Bytes

New Operator

If you want to make an instance of an inner class without making a method on the class containing it, you can use the "new operator."

Whereas you make an instance of a regular class by saying something like new ClassName(), you can make an instance of an inner class by using .new on a variable that holds an instance of the containing class.

That's a confusing verbal description, but it kinda makes sense once you see it.

class Car {
    class Speedometer {
    }
}
~class Car {
~    class Speedometer {
~    }
~}
class Main {
    void main() {
        Car car = new Car();
        Car.Speedometer speedometer = car.new Speedometer();
        IO.println(speedometer);
    }
}