• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

compile time errors, why?

 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

*/
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
reply
    Bookmark Topic Watch Topic
  • New Topic