0

I am writing a database manager. There are method for adding an car into database and removing car.

public class DBManager {

public static void addCar(Car car) {
        ...
    }

public static void deleteCar(Car car) {
        try {
            Statement statement = connection.createStatement();
            statement.executeUpdate("DELETE FROM cars WHERE id = '" + car.getId() + "'");
        } catch (SQLException e) {
            System.out.println(e);
        }
    }
}  

Next I have class Car:

public class Car {
        private int id;
        private String name;

    public User(String name) {
        this.name = name;
    }

    public int getId(){
    return this.id;}
    }

I create and save car in method main. When I want delete car, car is deleted from database but I have object car in a programme and I can work with it. How I can delete carA inside method deleteCar?

public class Main {
    public static void main(String[] args) {
       Car carA = new Car("Opel");

       DBManager.addCar(carA);
       DBManager.deleteCar(carA);

    }
}
 
Vlados
  • 151
  • 1
  • 4
  • 10
  • 1
    In your `main` method you can just do `carA = null;`. I guess it's not clear to me what specifically you're looking to accomplish from within the method. That method has no access to the `carA` variable. It can access the `Car` instance and make any modifications you like on that, such as calling a method which might set all fields to empty values or something of that nature. What specifically is the goal? – David Sep 15 '20 at 14:35
  • You can set carA object to null after deleting the carA from dB. – CodeBlooded Sep 15 '20 at 14:36
  • You can't. `deleteCar` receives a reference to the Car object. After the call variable carA will still point to the Car object. – Conffusion Sep 15 '20 at 14:36

0 Answers0