什么是变量可以是Netbeans的最终意思?

时间:2017-09-20 16:20:30

标签: java netbeans

我一直在尝试创建一个代码,用于在用户输入特定数据时计算windchill。我是Java的新手,并且在这个项目上遇到了很多困难。但是,我终于让它成功运行了。即使它现在正在运行,我仍然会收到消息,说我的变量可能是最终的。我不确定这意味着什么,也不能让它消失。我在评论中标记了那些有消息的人。有人可以解释这意味着什么以及为什么会发生这种情况?

这是我的代码:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        String line;
        String date;
        int windSpeed, temperature;

        Scanner kb = new Scanner(System.in);
        System.out.println("Enter the observation date: ");
        date = kb.nextLine();

        System.out.println ("Enter the wind speed in MPH: ");        
        line = kb.nextLine();
        windSpeed = Integer.parseInt(line);

        System.out.println ("Enter the temperature in degrees F: ");
        line = kb.nextLine();
        temperature = Integer.parseInt(line);

        Observation observation1 = new Observation(date,windSpeed,temperature);
        System.out.println("Observation Date: " + observation1.getDate());
        System.out.println("WindChill: " + observation1.getwindChill());

    }    
}

我在同一个包中的其他课程:

public class Observation {
    private String date; //can be final
    private int windSpeed;//can be final
    private int temperature;//can be final
    private double windChill;//can be final      

    public Observation (String d, int s, int t){
        date = d;
        windSpeed = s;
        temperature = t;

        windChill = 35.74 + 0.6215*t + (0.4275*t - 35.75) * Math.pow(s,0.16);

    }

    public String getDate (){
        return date;
    }

    public double getwindChill(){
        return windChill;
    }
}

1 个答案:

答案 0 :(得分:2)

Java中的最终变量是costants,这意味着一旦设置了它们的值,它就永远不会改变。 NetBeans建议您在以下情况下在变量声明中添加final修饰符:

  • 您在创建后立即分配值
  • 在构造函数中初始化它们

你永远不会改变他们的价值观。