最终变量可能已被分配 [英] Final variable may already have been assigned
问题描述
我有初始化与任何其他类变量相同,我希望在初始化名称和类型但不是值时使用。
public final String filename,filepath;
在构造函数中,我设置的值如下
public myClass(String value){
this.filename = value;
this.filepath = anotherPartOfValue;
}
当我这样做,我得到一个警告,最后的字段[x ]可能已经被分配了
有没有办法避免此警告,仍然保持最终状态并在构造函数中设置值?
我正在使用eclipse btw。
编辑:
这是给我错误的确切代码
import java.io. *;
public class Dirt {
private String [] tables;
private int numTables;
private final String filename,filepath;
public Dirt(String file){
this.tables = new String [0];
this.numTables = 0; (int i = file.length(); i< 0; i--){
if(file.charAt(i)=='/'){
this.filename = file.substring(i);
this.filepath = file.substring(1,i-1);
}
}
}
}
问题是你在一个循环中分配给最终的变量。没有什么可以阻止循环循环多次,并且如果
条件不止一次被满足。 (如果文件中有两个/字符,那么会发生什么?/ / code>?还是没有?)
是在构造函数中使用临时String变量,然后将它们分配给最终的 filename
和 filepath
:
public Dirt(String file){
this.tables = new String [0];
this.numTables = 0;
String name = null;
String path = null; (int i = file.length(); i< 0; i--){
if(file.charAt(i)=='/'){
name = file.substring(i);
path = file.substring(1,i-1);
//需要休息吗?
}
}
this.filename = name;
this.filepath = path;
}
这是丑陋的,但这是一个简单的方法来确定 filename
和 filepath
一定会被分配,绝对只能分配一次。
I am writing a program that has two final variables that I wish to use, I need these to be set at the time that I actually run the class because they are likely to be different each instance.
I have the initialization the same as any other class variable I wish to use where I initialize a name and type but not a value.
public final String filename, filepath;
In the Constructor I set the values as follows
public myClass(String value) {
this.filename = value;
this.filepath = anotherPartOfValue;
}
When I do this, I get a warning that "The final field [x] may have already been assigned"
Is there a way to avoid this warning and still keep the final state and set the value in the constructor?
I am using eclipse btw.
Edit:
This is the exact code that gives me the error
import java.io.*;
public class Dirt {
private String[] tables;
private int numTables;
private final String filename, filepath;
public Dirt(String file) {
this.tables = new String[0];
this.numTables = 0;
for (int i = file.length(); i < 0; i--) {
if (file.charAt(i) == '/') {
this.filename = file.substring(i);
this.filepath = file.substring(1, i-1);
}
}
}
}
The problem is that you are assigning to the final variables in a loop. There's nothing to prevent the loop from looping more than once and the if
condition being satisfied more than once. (What happens if there are two '/' characters in file
? Or none?)
One way around this is to use temporary String variables in the constructor and then assign them to filename
and filepath
at the end:
public Dirt(String file) {
this.tables = new String[0];
this.numTables = 0;
String name = null;
String path = null;
for (int i = file.length(); i < 0; i--) {
if (file.charAt(i) == '/') {
name = file.substring(i);
path = file.substring(1, i-1);
// need a break here?
}
}
this.filename = name;
this.filepath = path;
}
It's ugly, but it's a straightforward way to know for sure that filename
and filepath
will definitely be assigned and definitely be assigned only once.
这篇关于最终变量可能已被分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!