0

I have CarService.java class, List of car and getter inside. Like this:

public class CarService {
public Scanner scanner;
private List<Car> cars;
private List<Person> persons;
private List<BankAccount> accounts;


public List<Car> getcarlist() {
    return cars;
}
private void initalizeCars() {...}
   
   

Another Class Car.java. Here I want to use

CarService.getcarlist().add(rest...)

The error is: Non-static method 'getcarlist()' cannot be referenced from a static context

Mannualyy
  • 13
  • 3
  • 1
    Does this answer your question? [What is the reason behind "non-static method cannot be referenced from a static context"?](https://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static) – OH GOD SPIDERS Mar 30 '21 at 14:36
  • you need to create instance of CarService and use that referece to be able to call the method. Since, getcarlist() is not a static method, you cannot reference it using CarService .xxx() – akshaya pandey Mar 30 '21 at 15:03

0 Answers0