-2

Tried to run vehicle problem, debug a bit but found the problem is at Car joe = new Car(1234567, 'R', 4);

public class Vehicle {

    private int engineNum;
    private char color;

    public void setNum(int num) { engineNum=num; }
    public void setColor(char color) { this.color = color; }
    public void printVehicle() {
        System.out.println("Engine No.: " + engineNum);
        System.out.println("Vehicle Color: " + color);
    }

    class Car extends Vehicle {
        private int doors;
        public Car(int num, char color, int doors){
            setNum(num);
            setColor(color);
            this.doors = doors;
        }
        public void printCar() {
            System.out.println("=====Car Info=====");
            printVehicle();
            System.out.println("Car door no.: " + doors);
        }
    }

    public static void main(String[] args) {
        **Car joe = new Car(1234567, 'R', 4);**
        Car jane = new Car(5678924, 'B', 5);
        joe.setColor('W');
        joe.printCar();
        jane.printCar();
    }
}

With message: No enclosing instance of type Vehicle is accessible. Must qualify the allocation with an enclosing instance of type Vehicle (e.g. x.new A() where x is an instance of Vehicle).

Erick G. Hagstrom
  • 4,873
  • 1
  • 24
  • 38
Snowman
  • 59
  • 1
  • 10

1 Answers1

2

The problem here is that you have Car defined as an inner class of Vehicle. As such the Car object is tied to a particular instance of a Vehicle. At the moment when you try to create a Car it does not have an instance of Vehicle to be created in, hence the error. There are two options for solving this:

  • Make the Car class static. This option is not really applicable for your case here since you need different instances of Car which need to be able to access the Vehicle variables and methods.
  • Move the Car class outside of the Vehicle class. If it is defined not as an inner class you will be able to instantiate it without an instance of the Vehicle, in fact it will be it's own instance of Vehicle.
ewanc
  • 1,284
  • 1
  • 12
  • 23
  • What do you mean about the `static` option not being applicable here? One _can_ create instances of a `static` inner class more than once. – Erick G. Hagstrom Feb 17 '16 at 10:34
  • well a static nested class does not have a reference to the nesting class, so it will not be able to access the non-static Vehicle member variables or methods. Agree that I could have phrased it better. Answer updated to make this more clear. – ewanc Feb 17 '16 at 11:09
  • But it `extends Vehicle`, so it has `setNum()`, `setColor()` and `printVehicle()`. But you're right, it can't access the `private` member variables. – Erick G. Hagstrom Feb 17 '16 at 17:34
  • That said, the best option is just to move it outside of `Vehicle`. – Erick G. Hagstrom Feb 17 '16 at 17:35
  • Agreed. Regardless of the mechanics of inner classes in this case moving Car outside of Vehicle is the best solution – ewanc Feb 17 '16 at 17:39