分配最终变量后,是否可以更改它们?

时间:2019-11-15 03:20:48

标签: java arrays variables final

static_assert(sizeof(void*) <= sizeof(jlong), "Unsupported architecture");

错误:

import java.util.Scanner; public class OnlineShop { public static void main (String[]args) { final double subAmmount; subAmmount = subAmmount + Array[i]; }

2 个答案:

答案 0 :(得分:0)

“ subAmmount”尚未初始化/设置,因此您不能向其自身添加subAmmount,因为它不等于任何值。

答案 1 :(得分:-1)

由于subAmount未声明为static,因此必须在声明时进行初始化。所以你应该有这样的东西

import java.util.Scanner;

public class OnlineShop {

  public static void main (String[]args) {

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

如果您想先声明然后初始化,则可以这样做:

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
}

final关键字上仔细检查一下link