If else if statement and switch statement

I didn't understand the below output. I have created 4 instances and invoking methods and getting that below output 
My question is how the methods are invoking? I mean in which order? bc High value item! printed 4 times?

Can anyone explain the flow of the program?

https://www.codecademy.com/courses/learn-java/lessons/java-conditionals-and-control-flow/exercises/review

public class Order {
  boolean isFilled;
  double billAmount;
  String shipping;
  
  public Order(boolean filled, double cost, String shippingMethod) {
		if (cost > 24.00) {
      System.out.println("High value item!");
    } else {
      System.out.println("Low value item!");
    }
    isFilled = filled;
    billAmount = cost;
    shipping = shippingMethod;
  }
  
  public void ship() {
    if (isFilled) {
      System.out.println("Shipping");
    } else {
      System.out.println("Order not ready");
    }
    
    double shippingCost = calculateShipping();
    
    System.out.println("Shipping cost: ");
    System.out.println(shippingCost);
  }
  
  public double calculateShipping() {
    double shippingCost;
    switch (shipping) {
      case "Regular":
        shippingCost = 0;
        break;
      case "Express":    
        shippingCost = 1.75;
        break;
      default:
        shippingCost = .50; 
    }
    return shippingCost;
 	}
  
  public static void main(String[] args) {
    // create instances and call methods here!
    Order computer = new Order(true, 25, "Express");
    Order iphone = new Order(false, 50, "Regular");
    Order ipad = new Order(true, 75.15, "Express");
    Order note10 = new Order(false, 78.14, "Regular");

    computer.calculateShipping();
    iphone.ship();
    ipad.calculateShipping();
    note10.ship();
  }
}

O/P : 
High value item!
High value item!
High value item!
High value item!
Order not ready
Shipping cost: 
0.0
Order not ready
Shipping cost: 
0.0

As you know, main is what runs when the code is run. It is the default method.

The first thing it does is create an instance of the Order class.

Order computer = new Order(true, 25, "Express");

By default, when creating a class, a constructor or instantiate() method is called.

  • In Python, it is def __init__(self).
  • In PHP, it is public function__construct().
  • In Javascript, it is constructor().
  • In Java, it is a method with the same name as this class. In this case, it is Order(boolean filled, double cost, String shippingMethod).

When a new instance of the Order class is created, the constructor method public Order() is called. This method does what it is supposed to do. It sets the internal variables: isFilled, billAmount and shipping. Oddly, it also has an if statement in it that prints out “High value item!” or “Low value item!”. Is that supposed to be in the constructor method? I’d review the instructions.

The second mistake is in the calculateShipping() method. The constructor method (Order) sets the internal class variables: isFilled, billAmount and shipping. However, the CalculateShipping() method overrides the class variable with a local variable with the same name: double shippingCost. That line is an error.

Hope that helps.

I didn’t get any error message while I was executing. That was the output I got.

You didn’t make any syntax or naming errors. The code will run.

The problem is that you overwrote a class variable (class property) with a local variable.

public double calculateShipping() {
    double shippingCost;

ShippingCost is a class property accessible to all your methods. There is no need to redeclare it in any method. Doing so creates a varaible local to that method and overwrites the class property.