Re: Java symbol confusion
"Matt Humphrey" <matth@ivizNOSPAM.com> wrote in message
news:Wr-dnerVwNmJ9ajYnZ2dnUVZ_vGdnZ2d@adelphia.com...
"Constant Meiring" <icesslinux@gmail.com> wrote in message
news:1161127462.963642.75210@i42g2000cwa.googlegroups.com...
I started learning myself java a while ago and there's still loads of
concepts and things about the java language i don't know
[...]
my first problem here is that when the two lines marked with arrows
above is out of the try-catch block, NetBeans tells me it can't find
the symbol serverSocket. On the other hand, when I put the two marked
lines inside of the try-catch block, it works without a problem. Can
someone explain to me why this is happening??
The name "serverSocket" is a local variable and that name exists only from
the { of the try to the } before the catch. After that } the name no
longer exists, so you can't use it within the catch expression or
afterwards. Put ServerSocket = null; before the try { and remove the
ServerSocket declaration. The name will then exist for the entire method
body.
If you want to learn more about this concept, the name of this concept
is "scope". The scope of the variable is within that try block. When you
move your assignment statement outside of the try block, you're referring to
the variable outside of its scope, meaning it can't be found by the
compiler.
The "scope" concept isn't Java specific; it exists in most other
programming languages as well.
- Oliver