Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compile time errors, why?

 
abhay jain
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



/*
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
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the variables in and out aren't visible from the finally block. You need to declare them outside of the try block.
 
abhay jain
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic