0

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?

6
  • 5
    efficiency and fuel must be defined as members. In your example they are just local variables of the constructor. Commented Jul 17, 2019 at 14:56
  • @Benoit - How do I do this? Commented Jul 17, 2019 at 14:57
  • docs.oracle.com/javase/tutorial/java/javaOO/classes.html Commented Jul 17, 2019 at 14:59
  • Previous link was to the wrong page. New link should be correct. Commented Jul 17, 2019 at 14:59
  • @Slaw The previous link seemed to work for me! It did lead me to the correct answer... Commented Jul 17, 2019 at 15:00

3 Answers 3

4

You're using local variables in your Car's constructor. Their lifecycle bounded within your constructor. Just declare your members in your class and use getters and setters to access them.

class Car
{
    private double efficiency;
    private double fuel;

    public Car(double mpg, double initFuel)
    {
        this.efficiency = mpg;
        this.fuel = initFuel;
    }

    public void setEfficiency(double efficiency) {
        this.efficiency = efficiency;
    }

    public double getEfficiency() {
        return efficiency;
    }

    // Same thing for fuel...
}

And in your Main:

class Main
{
    public static void main(String[] args)
    {
        Car ferrari = new Car(18, 25.43);
        System.out.println(ferrari.getEfficiency());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

Make efficiency and fuel global to your class rather than local to your constructor. Main.java

public class Main
{
    public static void main(String[] args)
    {
        Car ferrari = new Car(18, 25.43);
        System.out.println(ferrari.efficiency);
    }
}

Car.java

public class Car
{
    public double efficiency;
    public double fuel;
    public Car(double mpg, double initFuel)
    {
        efficiency = mpg;
        fuel = initFuel;
    }
}

That should work. But you can make the instance variables private instead of public and use setters/getters Also set your Main and Car to public so they can be accessible/instantiated from another class. Main.java

public class Main
{
    public static void main(String[] args)
    {
        Car ferrari = new Car(18, 25.43);
        System.out.println(ferrari.getEfficiency());
    }
}

Car.java

public class Car
{
    private double efficiency;
    private double fuel;
    public Car(double mpg, double initFuel)
    {
        efficiency = mpg;
        fuel = initFuel;
    }
    public double getEfficiency(){
        return efficiency;
    }
}

Comments

3

Change your Car class to:

class Car
{ 
    private double efficiency;
    private double fuel;

    public Car(double mpg, double initFuel)
    {
        this.efficiency = mpg;
        this.fuel = initFuel;
    }
}

You need to declare the variables on the Class scope instead of the local scope when using a variable for an instantiated Object. These variables typically are set from the Constructor so they can be accessed later from the Object itself.

Most of the time these are also declared as private with a separate method to get and set them instead of directly accessing them.

Example:

public double getEfficiency(){
    return this.efficiency;    
}
public void setEfficiency(double mpg){
    this.efficiency = mpg;
}

Most IDE's can auto generate Getters and Setters for you so you do not need to hand write them for every variable.

To use the getter/setter you call the method from an instance of the class rather than using the variable directly:

Car tesla = new Car(35.5, 90.5);

tesla.efficiency = 15.5; //YOU CAN'T DO THIS
tesla.setEfficiency(15.5); //Do this instead

System.out.println(tesla.getEfficiency()); //Will print 15.5

1 Comment

Wrong return type in the getter/setter

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.