我在一个类中有一个字符串,我想在另一个类中使用它,但我无法编译它
我下面的代码应该从另一个java类中提取NewDestination,但是我得到错误变量NewDestination可能没有被初始化,当我尝试运行时,我该如何初始化它?
@Named("Filter")
public class Filter{
//public String NewDestination;
private static final String FILE_TEXT_EXT = ".pdf";
public static final String NewDestination;
public static void main(String args[]) {
new Filter().listFile(NewDestination, FILE_TEXT_EXT);
}
public void listFile(String folder, String ext) {
GenericExtFilter filter = new GenericExtFilter(ext);
File dir = new File(folder);
if(dir.isDirectory()==false){
System.out.println("Directory does not exists : " + NewDestination);
return;
}
// list out all the file name and filter by the extension
String[] list = dir.list(filter);
if (list.length == 0) {
System.out.println("no files end with : " + ext);
return;
}
for (String file : list) {
String temp = new StringBuffer(NewDestination).append(File.separator)
.append(file).toString();
System.out.println("file : " + temp);
}
}
// inner class, generic extension filter
public class GenericExtFilter implements FilenameFilter {
private String ext;
public GenericExtFilter(String ext) {
this.ext = ext;
}
public boolean accept(File dir, String name) {
return (name.endsWith(ext));
}
}
}