-2
import java.util.Scanner;

public class OnlineShop {

  public static void main (String[]args) {

    final double subAmmount;

    subAmmount = subAmmount + Array[i];
}

The error:

The final local variable subAmmount may have already been assigned

Yogesh Badke
  • 4,249
  • 2
  • 15
  • 23
Bob
  • 1

2 Answers2

0

"subAmmount" has not been initialized/set yet, so you cannot add subAmmount to itself, since it is not equal to anything.

Ark1409
  • 25
  • 6
-1

Because subAmount is not declared as static, it must be initialized at the time of declaration. So you should have something like this

import java.util.Scanner;

public class OnlineShop {

  public static void main (String[]args) {

    final double subAmmount = Array[i]; // Assuming Array[i] is defined somewhere
}

If you'd like to do declaration followed by initialization, you'd do:

import java.util.Scanner;

public class OnlineShop {

  static final double subAmmount;

  public static void main (String[]args) {

    subAmmount = Array[i]; // Assuming Array[i] is defined somewhere
}

Checkout this link for more nuances on the final keyword

Kwame Opare Asiedu
  • 2,110
  • 11
  • 13