aspose file tools*
The moose likes Beginning Java and the fly likes compile time errors, why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compile time errors, why?" Watch "compile time errors, why?" New topic
Author

compile time errors, why?

abhay jain
Ranch Hand

Joined: Jun 03, 2011
Posts: 130



/*
C:\>javac CopyCharacters2.java
CopyCharacters2.java:30: cannot find symbol
symbol : variable in
location: class CopyCharacters2
in.close();
^
CopyCharacters2.java:31: cannot find symbol
symbol : variable out
location: class CopyCharacters2
out.close();
^
2 errors

*/
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

Think about the scope of the variables you declare on lines 11 and 12.

(I've added code tags to your post - it makes it much more readable. You can do it yourself - see UseCodeTags for more details).
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Because the variables in and out aren't visible from the finally block. You need to declare them outside of the try block.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
abhay jain
Ranch Hand

Joined: Jun 03, 2011
Posts: 130
Wouter Oet wrote:Because the variables in and out aren't visible from the finally block. You need to declare them outside of the try block.



still having problem with that, can you please put that improved new code of this program for me
abhay jain
Ranch Hand

Joined: Jun 03, 2011
Posts: 130
Matthew Brown wrote:Think about the scope of the variables you declare on lines 11 and 12.




need some more detail, if possible . thanks for response
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

If you declare a variable in a method, you can't access it outside that method, right? That's what I mean by scope - you can only refer to a variable if it's in scope. Well, it's the same with any block (delimited by {...}). The variables you declare on line 9/10 are only in scope in the lines 9-16. Once you're outside that block, they're gone.

So the answer is, declare the variables outside the block. Move your declarations (but not the creation of the objects) outside the try block:

One more thing you now have to look out for, though: in the case of a failure to create those objects, the variables will still be null. So you need to cope with that possibility when you try to close them.
abhay jain
Ranch Hand

Joined: Jun 03, 2011
Posts: 130
Matthew Brown wrote:If you declare a variable in a method, you can't access it outside that method, right? That's what I mean by scope - you can only refer to a variable if it's in scope. Well, it's the same with any block (delimited by {...}). The variables you declare on line 9/10 are only in scope in the lines 9-16. Once you're outside that block, they're gone.

So the answer is, declare the variables outside the block. Move your declarations (but not the creation of the objects) outside the try block:

One more thing you now have to look out for, though: in the case of a failure to create those objects, the variables will still be null. So you need to cope with that possibility when you try to close them.




thanks a lot
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

But beware that now in and out can both be null during the finally block, so build in a check that only closes the streams when they aren't null.

Or use my favourite technique - nested try statements:
The inner-most try-finally is to ensure out gets closed. Note that out gets initialized outside of it.
The try-finally around that is to ensure in gets closed. Note that in gets initialized outside of it.
The outer-most try-catch is to catch any IOException that is thrown - including those from in.close() and out.close().


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compile time errors, why?