-6

I have a Vehicle superclass and then I have some subclasses to that one. I am trying to have an array list that adds objects from all of the subclasses into the same list, but I'm coming up short.

I currently have the list in my program class but I have moved it around a bit.

This is the code I have in Program:

ArrayList<Vehicles> Inventorie = new ArrayList<>();

public void setInventorie(Car newCar) {
    Inventorie.add(newCar);
}

And this the code I have in Car:

public void addCar(){
    System.out.println("Make: ");
    Make = scan.nextLine();
    System.out.println("RegNr");
    regNr = scan.nextInt();
    System.out.println("Year");
    year = scan.nextInt();
    setValue();
    Car c1 = new Car(make, regNr, Year);
     Program.setInventorie(c1);
}

In Car I am getting "Non-static method 'setInventorie(Project1.Vehicles)' cannot be referenced from static context."

I also had to comment out the main method or the compiler couldn't resolve the symbol Inventorie (name of array list).

Tried to explain my problem as best as I could, hope someone can help. And yes, I am new to this so it might seem really stupid but it's the best I could do.

Thanks

brodude
  • 23
  • 5
  • 1
    You should study the [difference between static methods and instance methods](http://stackoverflow.com/questions/11993077/difference-between-static-methods-and-instance-methods). You defined `setInventorie()` as an instance method but you're using it as a class (static) method. That's not allowed in Java – Oneiros Apr 04 '17 at 10:53
  • And read about java naming conventions. Only class names or constants start with UpperCase; variables go carInventory always ... – GhostCat Apr 04 '17 at 10:55

1 Answers1

0

I'm guessing it's the line Program.setInventorie(c1);. You need to create an instance of the Program class and call setInventorie() on that. setInventorie() is not a static method.

e.g.:

Program myProgram = new Program();
myProgram.setInventorie(c1);
Steve Smith
  • 2,244
  • 2
  • 18
  • 22