-2

I was following our teachers instructions but even still the program runs into a problem where it says that both CONVERTABLE and BLACK cannot be resolved or is not a field.

public class CarDemo {

public static void main(String[] args) {
    Car car1 = new Car(2014, Model.CONVERTABLE, Color.BLACK);
    
}
class Car{
    private int year;
    private Model model;
    private Color color;
    public Car(int yr, Model model, Color color){
        year = yr;
        this.model = model;
        this.color = color;   
    }
    public void display(){
        System.out.println(year);
        System.out.println(model);
        System.out.println(color);
    }
}
class Model{
    enum model {SEDAN, SUV, CONVERTABLE, HATCHBACK;}
}
class Color{
    enum color {RED, BLUE, VIOLET, PINK, BLACK, WHITE;}

} }

Another version of the code when trying to fix the code was this

public class CarDemo {

public static void main(String[] args) {
    Car car1 = new Car(2014, Model.model.CONVERTABLE, Color.color.BLACK);
    
}
class Car{
    private int year;
    private CarDemo.Model.model model;
    private CarDemo.Color.color color;
    public Car(int yr, CarDemo.Model.model m, CarDemo.Color.color c){
        year = yr;
        model = m;
        color = c;   
    }
    public void display(){
        System.out.println(year);
        System.out.println(model);
        System.out.println(color);
    }
}
class Model{
    enum model {SEDAN, SUV, CONVERTABLE, HATCHBACK;}
}
class Color{
    enum color {RED, BLUE, VIOLET, PINK, BLACK, WHITE;}

} }

but still didn't work and a new error came out it said No enclosing instance of type CarDemo is accessible

vromm
  • 1
  • 1
  • Does this answer your question? [What causes error "No enclosing instance of type Foo is accessible" and how do I fix it?](https://stackoverflow.com/questions/9560600/what-causes-error-no-enclosing-instance-of-type-foo-is-accessible-and-how-do-i) – Torben May 23 '22 at 05:33

1 Answers1

0

First use Outer class constructor for initialising the object of Inner class. Also enums should be in the static class.

public class CarDemo {

    public static void main(String[] args) {
        CarDemo.Car car1 = new CarDemo().new Car(2014, Model.model.CONVERTABLE, Color.color.BLACK);
        car1.display();
    }

    class Car {
        private int year;
        private CarDemo.Model.model model;
        private CarDemo.Color.color color;

        public Car(int yr, CarDemo.Model.model m, CarDemo.Color.color c) {
            year = yr;
            model = m;
            color = c;
        }

        public void display() {
            System.out.println(year);
            System.out.println(model);
            System.out.println(color);
        }
    }

    static class Model {
        enum model {
            SEDAN, SUV, CONVERTABLE, HATCHBACK;
        }
    }

    static class Color {
        enum color {
            RED, BLUE, VIOLET, PINK, BLACK, WHITE;
        }
    }
}
psybrg
  • 689
  • 5
  • 7