Java symbol confusion  
Author Message
Constant Meiring





PostPosted: 2006-10-18 7:24:00 Top

java-programmer, Java symbol confusion Hi there,

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, so i may just
make an arse of myself now...

I use the NetBeans 5.0 IDE to play around with. Sometimes it does
things that I think (from what I've learned about java) that it
shouldn't do. I may just be stupid or something, so could somebody
point out my faults. Here's a piece of code:

import java.net.*;
import java.io.*;

public class Main {

public static void main(String[] args) {

try {
ServerSocket serverSocket = new ServerSocket(56);

}catch (IOException e) {System.out.println("Daar was kak in die
konneksie...");}

Socket clientSocket = null;
<<------------------------------------
clientSocket = serverSocket.accept();
<<------------------------------------
}
}

Ok that's just part of it. I'm still learning how to do the rest. Ok,
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??

===========WORKING=============
try {
ServerSocket serverSocket = new ServerSocket(56);
Socket clientSocket = null;
clientSocket = serverSocket.accept();
}catch (IOException e) {System.out.println("Daar was kak in die
konneksie...");}

===========NOT WORKING=============

try {
ServerSocket serverSocket = new ServerSocket(56);
}catch (IOException e) {System.out.println("Daar was kak in die
konneksie...");}
Socket clientSocket = null;
clientSocket = serverSocket.accept();

It says:

C:\Documents and
Settings\Constant\netServer\src\netserver\Main.java:22: cannot find
symbol
symbol : variable serverSocket
location: class Main
clientSocket = serverSocket.accept();
1 error
BUILD FAILED (total time: 0 seconds)

 
Matt Humphrey





PostPosted: 2006-10-18 7:35:00 Top

java-programmer >> Java symbol confusion
"Constant Meiring" <email***@***.com> wrote in message
news:email***@***.com...
> Hi there,
>
> 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, so i may just
> make an arse of myself now...
>
> I use the NetBeans 5.0 IDE to play around with. Sometimes it does
> things that I think (from what I've learned about java) that it
> shouldn't do. I may just be stupid or something, so could somebody
> point out my faults. Here's a piece of code:
>
> import java.net.*;
> import java.io.*;
>
> public class Main {
>
> public static void main(String[] args) {
>
> try {
> ServerSocket serverSocket = new ServerSocket(56);
>
> }catch (IOException e) {System.out.println("Daar was kak in die
> konneksie...");}
>
> Socket clientSocket = null;
> <<------------------------------------
> clientSocket = serverSocket.accept();
> <<------------------------------------
> }
> }
>
> Ok that's just part of it. I'm still learning how to do the rest. Ok,
> 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.

Matt Humphrey email***@***.com http://www.iviz.com/


 
Oliver Wong





PostPosted: 2006-10-20 2:12:00 Top

java-programmer >> Java symbol confusion "Matt Humphrey" <email***@***.com> wrote in message
news:email***@***.com...
>
> "Constant Meiring" <email***@***.com> wrote in message
> news:email***@***.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


 
 
Constant Meiring





PostPosted: 2006-10-23 19:37:00 Top

java-programmer >> Java symbol confusion Thanx now I know why you have to put an object = null sometimes.

Thanx alot!

-Constant

Matt Humphrey wrote:
> "Constant Meiring" <email***@***.com> wrote in message
> news:email***@***.com...
> > Hi there,
> >
> > 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, so i may just
> > make an arse of myself now...
> >
> > I use the NetBeans 5.0 IDE to play around with. Sometimes it does
> > things that I think (from what I've learned about java) that it
> > shouldn't do. I may just be stupid or something, so could somebody
> > point out my faults. Here's a piece of code:
> >
> > import java.net.*;
> > import java.io.*;
> >
> > public class Main {
> >
> > public static void main(String[] args) {
> >
> > try {
> > ServerSocket serverSocket = new ServerSocket(56);
> >
> > }catch (IOException e) {System.out.println("Daar was kak in die
> > konneksie...");}
> >
> > Socket clientSocket = null;
> > <<------------------------------------
> > clientSocket = serverSocket.accept();
> > <<------------------------------------
> > }
> > }
> >
> > Ok that's just part of it. I'm still learning how to do the rest. Ok,
> > 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.
>
> Matt Humphrey email***@***.com http://www.iviz.com/

 
 
Oliver Wong





PostPosted: 2006-10-23 22:19:00 Top

java-programmer >> Java symbol confusion [post re-ordered]

"Constant Meiring" <email***@***.com> wrote in message
news:email***@***.com...
> Matt Humphrey wrote:
>> "Constant Meiring" <email***@***.com> wrote in message
>> news:email***@***.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, so i may just
>> > make an arse of myself now...
>> >
[...]
>> > 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.
>>
>
> Thanx now I know why you have to put an object = null sometimes.

You only ever need to put an object = null if you actually want that
object to be null. If you want to declare a variable to exist at a certain
scope level, but not assign to it, you can do that too:

public class Main {
public static void main(String[] args) {
ServerSocket serverSocket; /*Don't assign any value to it yet.*/
try {
serverSocket = new ServerSocket(56);
} catch (IOException e) {
System.out.println("Couldn't create server socket. Aborting.");
System.exit(-1);
}
clientSocket = serverSocket.accept(); /*presumably, clientSocket was
declared somewhere*/
}
}

- Oliver