$ javac getalldirs.java getalldirs.java:16: cannot find symbol symbol : variable checkfile location: class getalldirs system.out.println(checkfile.getname()); ^ 1 error $ cat getalldirs.java import java.util.*; import java.io.*; public class getalldirs { public void getalldirs(file file) { if(file.isdirectory()){ system.out.println(file.getname()); file checkfile = new file(file.getcanonicalpath()); }else if(file.isfile()){ system.out.println(file.getname()); file checkfile = new file(file.getparent()); }else{ // checkfile should initialized @ least here! file checkfile = file; } system.out.println(file.getname()); // why error here: checkfile not found system.out.println(checkfile.getname()); } public static void main(string[] args) { getalldirs dirs = new getalldirs(); file current = new file("."); dirs.getalldirs(current); } }
jls 14.4.2 scope of local variable declarations:
the scope of local variable declaration in block rest of block in declaration appears, starting own initializer , including further declarators right in local variable declaration statement.
jls 14.2 blocks
a block sequence of statements, local class declarations , local variable declaration statements within braces.
the way declared , initialized checkfile
, they're 3 separate local variables goes out of scope @ end of respective blocks.
you can fix putting declaration of file checkfile;
first line of getalldirs
method; puts scope rest of method.
similar questions
- variable cannot resolved
- made trickier because variable array, has special initialization shorthand syntax
Comments
Post a Comment