Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use another function to create objects

Tags:

java

I'm fairly new to Java, and I was making a small application that takes two cars and returns who is faster. I made another class for the cars. First I made all the objects in the main method and it worked, then I tried putting the code in another method and called that from main, just to make it neater, and I got an error. Probably something obvious, but I'm too tired to think straight.

Error:(7, 14) java: cannot find symbol symbol: variable ferrari location: class Test

Error:(7, 23) java: cannot find symbol symbol: variable lamborghini location: class Test

public class Test {

public static void main(String[] args) {

    createCars();

    race(ferrari, lamborghini);

}

public static void createCars() {

    Car ferrari = new Car("Ferrari", "California");
    ferrari.setHp(552);
    ferrari.setAcceleration(3.3);

    Car lamborghini = new Car("Lamborghini", "Huracan");
    lamborghini.setHp(602);
    lamborghini.setAcceleration(2.5);

    Car bmw = new Car("BMW", "M5");
    bmw.setHp(560);
    bmw.setAcceleration(3.7);

    Car cadillac = new Car("Cadillac", "CTS-V");
    cadillac.setHp(640);
    cadillac.setAcceleration(3.6);

}


public static void race(Car carA, Car carB) {
    if (carA.getAcceleration() < carB.getAcceleration()) {
        System.out.println("The " + carA.getMake() + " " + carA.getModel() + " is faster than the " + carB.getMake() + " " + carB.getModel());
    } else if (carB.getAcceleration() < carA.getAcceleration()) {
        System.out.println("The " + carB.getMake() + " " + carB.getModel() + " is faster than the " + carA.getMake() + " " + carA.getModel());
    } else {
        System.out.println("It's a tie");
    }
}

}

like image 723
Emil Øgård Avatar asked May 30 '26 18:05

Emil Øgård


2 Answers

You get that error because your car instances ferrari and lamborghini are not in the scope of the main function.

You may modify your create cars method to it returns a new car insteand:

public static Car createCar(String make, String model, int hp, double acceleration ) {
  Car car = new Car(make, model);
  car.setHp(hp);
  car.setAcceleration(acceleration);
  return car;
}

and then you can use it:

public static void main(String[] args) {

    Car ferrari = createCar("Ferrari", "California", 552, 3.3 );
    Car lamborghini = createCar("Lamborghini", "Huracan", 602, 2.5 );
    race(ferrari, lamborghini);

}

... or directly

public static void main(String[] args) {
    race( 
        createCar("Ferrari", "California", 552, 3.3 ),
        createCar("Lamborghini", "Huracan", 602, 2.5 )
    );
}
like image 174
OscarRyz Avatar answered Jun 01 '26 07:06

OscarRyz


The objects that you create become unreachable (and therefore ready for garbage collection) as soon as the method in which you created them is over. This is because you assign them to local variables inside that method, and no other part of your program has access to these variables.

You need a way for the rest of your program to access the cars you created. One way is to make an object that "holds" them, like this:

class Garage {
    private Car ferrari;
    private Car lamborghini;
    private Car bmw;
    private Car cadillac;
    // You could use a constructor instead of using a separate method.
    public void createCars() {
        ferrari = new Car("Ferrari", "California");
        ferrari.setHp(552);
        ferrari.setAcceleration(3.3);

        lamborghini = new Car("Lamborghini", "Huracan");
        lamborghini.setHp(602);
        lamborghini.setAcceleration(2.5);

        bmw = new Car("BMW", "M5");
        bmw.setHp(560);
        bmw.setAcceleration(3.7);

        cadillac = new Car("Cadillac", "CTS-V");
        cadillac.setHp(640);
        cadillac.setAcceleration(3.6);
    }
    public void getFerrari() {return ferrari;}
    public void getLamborghini() {return lamborghini;}
    public void getBmw() {return bmw;}
    public void getCadillac() {return cadillac;}
}

Now you can use the Garage class in your main() method, like this:

public static void main(String[] args) {
    Garage garage = new Garage();
    // If you move createCars into Garage's constructor, you wouldn't need to call createCars any longer.
    garage.createCars();

    race(garage.getFerrari(), garage.GetLamborghini());
}
like image 35
Sergey Kalinichenko Avatar answered Jun 01 '26 09:06

Sergey Kalinichenko