Q:

A method can't read array elements from main method. Why?

public static void main(String[] args) {

    Cars[] cars = new Cars[5];
    cars[0] = new Cars("BMW", 318, "SomeText");

    carDetails(0);
}

private static void carDetails( int index ) {
    System.out.println( cars[index].getName() + " "
                        + cars[index].getModel() + " "
                        + cars[index].getSomeText());
}

I got a seperated Car class with a constructor(String, int, String)
and fields and accessors. I cant compile this because I got error
in carDetails method. (cars[index]....) It says:
cannot find symbol
symbol: variable car
location: clas Main
Whhy I can't see cars elements in carDetails method?
Thank you in advance!

A:

Code in one method can't see variables in another method. If you need the variables accessible elsewhere, make them static members of your class.
class Foo {
    private static Car[] cars;
    public static void main(String[] args) {
         cars = new Car[5];
         cars[0] = new Car("BMW", 318, "SomeText");
         carDetails(0);
    }
    private static void carDetails( int index ) {
       System.out.println( cars[index].getName() + " "
                    + cars[index].getModel() + " "
                    + cars[index].getSomeText());
    }
}

