I have two classes, Main.java and Car.java
Main.java:
class Main
{
    public static void main(String[] args)
    {
        Car ferrari = new Car(18, 25.43);
        System.out.println(ferrari.efficiency);
    }
}
Car.java:
class Car
{
    public Car(double mpg, double initFuel)
    {
        double efficiency = mpg;
        double fuel = initFuel;
    }
}
I obviously tried to assign efficiency to the first constructor passed in when creating the object, but that doesn't seem to work (i.e. the variable is not found when I use the println in Main.java). How do I assign variables to objects to be referenced later?




efficiencyandfuelmust be defined as members. In your example they are just local variables of the constructor.