0

I'm trying to write a Java code which finds all files containing a determined a substring in the filename. That substring is a dynamic input of the program so it's stored in a string variable, named here "log3":

File fl = new File(dir); //fl is the directory in which look for files
File[] matchingFiles = fl.listFiles(new FileFilter() {
    public boolean accept(File x) {
         return (x.getName().contains(log3));
    }
});

The problem is that when I compile the code I get this error:

 local variable log3 is accessed from within inner class; needs  to be
 declared final
                 return (x.getName().contains(log3);
                                                             ^
NoobEditor
  • 15,563
  • 19
  • 81
  • 112
  • 8
    declare `log3` as `final String` instead of `String`. – Arnaud Denoyelle Jul 23 '14 at 13:54
  • possible duplicate of [How to pass parameters to anonymous class?](http://stackoverflow.com/questions/5107158/how-to-pass-parameters-to-anonymous-class) –  Jul 23 '14 at 14:02

4 Answers4

0

A local variable that belongs to the outer class is not visible from the inner class except if it is declared final.

Example :

private class Foo {

  private int v1; //Visible from the inner class because it is an attribute.

  public void bar() {

    int v2 = true; //Not visible from the inner class
    final boolean v3 //Visible from the inner class

    new InnerClass() {

      public void overridenMethod() {
        //Here, you can use v1 and v3 but not v2

      }
    }
  }
}

Your variable log3 must be declared as final.

Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
0

I understand that you can not declare log3 as final as it is changed within scope of your program. to handle this you can do it like this:

final String nameSubstring = log3;
File fl = new File(dir); //fl is the directory in which look for files
File[] matchingFiles = fl.listFiles(new FileFilter() {
    public boolean accept(File x) {
         return (x.getName().contains(nameSubstring));
    }
});
Sanjeev
  • 9,876
  • 2
  • 22
  • 33
0

I came across exactly the same problem today, and figured it out by the error message. It says clearly in the error message: Just declare log3 as final in your outer class.

Viktor Mellgren
  • 4,318
  • 3
  • 42
  • 75
0

Try to implement your own FileFilter, so you dont have to make the field final:

public class FilesTest {

 public static void main(String[] args) {
     new FilesTest();
 }

 public FilesTest() {

     File fl = new File("D:/");
     File[] matchingFiles = fl.listFiles(new CustomFileFilter("A."));
     for (File file : matchingFiles) {
          System.out.println(file.getAbsolutePath());
     }
 }

 class CustomFileFilter implements FileFilter {

     private String pattern;

     public CustomFileFilter(String pattern) {
         this.pattern = pattern;
     }

     public boolean accept(File x) {
         return (x.getName().contains(pattern));
     }
 }
}
Stefan
  • 12,108
  • 5
  • 47
  • 66