-
consequences of using static variables?
OK, I have a Status class that contains static variables. It can only be
accessed by get and set methods.
Since I have to run two copies of my collaboration software clients to test
if the clients work, I copied the classes into another directory. (I assume
that each client will then run in it's own space instead of referring to the
same Status class). I have a login server and a chat server running as well.
Scenario
Client A (in directory PCClientA) starts and login to login server. Login
server retrieves username "John" to the client. Works as I expect it would
be. Open chat window and I get to see the word "John : connecting to chat
server...".
Client B (in directory PCClientB) starts and login to login server. Login
server retrieves username "Jane" to the client. Works as I expect it would
be. Open chat window and I get to see the word "Jane : connecting to chat
server...".
I use back Client A to type something in. I expect to see "John : I'm typing
something to you Jane...". However, I get to see the username is not "John",
but rather "Jane". The result " Jane : I'm typing something to you Jane...".
Somehow, although I have two different directories and the Status class is
not public, I assume it can only be accessed by classes in the same
directory. But this is not the case.
I'm considering of running two clients on two different computers just to
make sure it has only one instance of the client in each computer.
Questions
1. Why is the variable in the Status class gets modified? I know I have
declared them static, but the class itself is not public. And I do have two
different directories.
2. Will it be better to test this case with two different computers?
--
Best Regards,
Wing Hoe
---------------------------------------------------------------
ICQ: 2213281
Email: winghoe@hotmail.com
www: http://pwp.maxis.net.my/winghoe
---------------------------------------------------------------
-
Re: consequences of using static variables?
Lim Wing Hoe,
At this point, since I cannot see the code, I can make an educated guess.
Going by your description, when you are creating
your user class you may have many instances of the subclass, which contains
the static name variable. Since all classes will share that same static name
variable, when you start off with "Tom" and then change it to "Mary" you
are changing the only copy of that static variable.
Here is a pseudocode example
oUser.setName("Tom");
oUser.setCity("Pittsburgh");
System.out.println(oUser.getName);
System.out.println(oUser.getCity);
We would see:
Tom
Pittsburgh
oUser2.setName("Jane");
oUser2.setCity("Orlando");
System.out.println(oUser2.getName);
System.out.println(oUser2.getCity);
We would see:
Jane
Orlando
Now, assuming Name was a static variable, we would attempt to view Tom's
information.
System.out.println(oUser.getName);
System.out.println(oUser.getCity);
We would see:
Jane
Pittsburgh
In this case, regardless of which object's getName method we call, it will
always return the single value held in the static name variable.
In general, Static variables are used to hold a value that will common to
all instances of your class; one such use is a static counter variable to
indicate the number of instances instantiated.
This may or may not be the solution to your problem. If nothing else, I hope
it will point you in the right direction.
Rich
-------------------------------------------------------------
"Lim Wing Hoe" <winghoe@hotmail.com> wrote:
>OK, I have a Status class that contains static variables. It can only be
>accessed by get and set methods.
>Since I have to run two copies of my collaboration software clients to test
>if the clients work, I copied the classes into another directory. (I assume
>that each client will then run in it's own space instead of referring to
the
>same Status class). I have a login server and a chat server running as well.
>
>Scenario
>Client A (in directory PCClientA) starts and login to login server. Login
>server retrieves username "John" to the client. Works as I expect it would
>be. Open chat window and I get to see the word "John : connecting to chat
>server...".
>
>Client B (in directory PCClientB) starts and login to login server. Login
>server retrieves username "Jane" to the client. Works as I expect it would
>be. Open chat window and I get to see the word "Jane : connecting to chat
>server...".
>
>I use back Client A to type something in. I expect to see "John : I'm typing
>something to you Jane...". However, I get to see the username is not "John",
>but rather "Jane". The result " Jane : I'm typing something to you Jane...".
>
>Somehow, although I have two different directories and the Status class
is
>not public, I assume it can only be accessed by classes in the same
>directory. But this is not the case.
>
>I'm considering of running two clients on two different computers just to
>make sure it has only one instance of the client in each computer.
>
>Questions
>1. Why is the variable in the Status class gets modified? I know I have
>declared them static, but the class itself is not public. And I do have
two
>different directories.
>2. Will it be better to test this case with two different computers?
>
>
>--
>
>
>
>Best Regards,
>Wing Hoe
>---------------------------------------------------------------
>ICQ: 2213281
>Email: winghoe@hotmail.com
>www: http://pwp.maxis.net.my/winghoe
>---------------------------------------------------------------
>
>
>
>
-
Re: consequences of using static variables?
Hello Rich,
I know the usage of static variables.
But since the class is sorta protected and in one particular directory
let's say
c:\myprogram\myprogram1\Status.class
class Status
{ static userName="";
......
}
And I have the same class in another directory
c:\myprogram\myprogram2\Status.class
Shouldn't both of these objects run differently? Since they're in two
different directories. I expect that when I have _class Status_ it means
that Status class is automatically added _protected_ modifier instead of
private or public. And since it's protected, according to the rule, this
class can only be accessed by the other classes in the same directory isn't
it?
I expect that these two objects that are used by the main app will not cross
each other's boundary and won't overwrite each other's static variables.
Isn't this true?
--
Best Regards,
Wing Hoe
---------------------------------------------------------------
ICQ: 2213281
Email: winghoe@hotmail.com
www: http://pwp.maxis.net.my/winghoe
---------------------------------------------------------------
"Rich" <cbuilder@NOSPAMhotbot.com> wrote in message
news:39f97a73@news.devx.com...
>
> Lim Wing Hoe,
>
> At this point, since I cannot see the code, I can make an educated guess.
> Going by your description, when you are creating
> your user class you may have many instances of the subclass, which
contains
> the static name variable. Since all classes will share that same static
name
> variable, when you start off with "Tom" and then change it to "Mary" you
> are changing the only copy of that static variable.
>
> Here is a pseudocode example
>
> oUser.setName("Tom");
> oUser.setCity("Pittsburgh");
>
> System.out.println(oUser.getName);
> System.out.println(oUser.getCity);
>
> We would see:
> Tom
> Pittsburgh
>
> oUser2.setName("Jane");
> oUser2.setCity("Orlando");
>
> System.out.println(oUser2.getName);
> System.out.println(oUser2.getCity);
>
> We would see:
> Jane
> Orlando
>
> Now, assuming Name was a static variable, we would attempt to view Tom's
> information.
>
> System.out.println(oUser.getName);
> System.out.println(oUser.getCity);
>
> We would see:
> Jane
> Pittsburgh
>
> In this case, regardless of which object's getName method we call, it will
> always return the single value held in the static name variable.
>
> In general, Static variables are used to hold a value that will common to
> all instances of your class; one such use is a static counter variable to
> indicate the number of instances instantiated.
>
> This may or may not be the solution to your problem. If nothing else, I
hope
> it will point you in the right direction.
>
> Rich
-
Re: consequences of using static variables?
Wing Hoe,
I think all of the instances of your classes are instantiated as a single
class in the VM. It would seem, regardless of location, the VM would see
these classes as identical classes.
As an experiment, you may want to try printing out some debug information
to let you know which class is being instantiated, the values of its properties
and the class location.
Good luck!
Rich
---------------------------------------------------------------
"Lim Wing Hoe" <winghoe@hotmail.com> wrote:
>Hello Rich,
>I know the usage of static variables.
>
>But since the class is sorta protected and in one particular directory
>let's say
>c:\myprogram\myprogram1\Status.class
>
>class Status
>{ static userName="";
>......
>}
>
>And I have the same class in another directory
>c:\myprogram\myprogram2\Status.class
>
>Shouldn't both of these objects run differently? Since they're in two
>different directories. I expect that when I have _class Status_ it means
>that Status class is automatically added _protected_ modifier instead of
>private or public. And since it's protected, according to the rule, this
>class can only be accessed by the other classes in the same directory isn't
>it?
>
>I expect that these two objects that are used by the main app will not cross
>each other's boundary and won't overwrite each other's static variables.
>Isn't this true?
>--
>
>
>
>Best Regards,
>Wing Hoe
>---------------------------------------------------------------
>ICQ: 2213281
>Email: winghoe@hotmail.com
>www: http://pwp.maxis.net.my/winghoe
>---------------------------------------------------------------
>
>
>"Rich" <cbuilder@NOSPAMhotbot.com> wrote in message
>news:39f97a73@news.devx.com...
>>
>> Lim Wing Hoe,
>>
>> At this point, since I cannot see the code, I can make an educated guess.
>> Going by your description, when you are creating
>> your user class you may have many instances of the subclass, which
>contains
>> the static name variable. Since all classes will share that same static
>name
>> variable, when you start off with "Tom" and then change it to "Mary" you
>> are changing the only copy of that static variable.
>>
>> Here is a pseudocode example
>>
>> oUser.setName("Tom");
>> oUser.setCity("Pittsburgh");
>>
>> System.out.println(oUser.getName);
>> System.out.println(oUser.getCity);
>>
>> We would see:
>> Tom
>> Pittsburgh
>>
>> oUser2.setName("Jane");
>> oUser2.setCity("Orlando");
>>
>> System.out.println(oUser2.getName);
>> System.out.println(oUser2.getCity);
>>
>> We would see:
>> Jane
>> Orlando
>>
>> Now, assuming Name was a static variable, we would attempt to view Tom's
>> information.
>>
>> System.out.println(oUser.getName);
>> System.out.println(oUser.getCity);
>>
>> We would see:
>> Jane
>> Pittsburgh
>>
>> In this case, regardless of which object's getName method we call, it
will
>> always return the single value held in the static name variable.
>>
>> In general, Static variables are used to hold a value that will common
to
>> all instances of your class; one such use is a static counter variable
to
>> indicate the number of instances instantiated.
>>
>> This may or may not be the solution to your problem. If nothing else,
I
>hope
>> it will point you in the right direction.
>>
>> Rich
>
>
>
-
Re: consequences of using static variables?
Rich <Rich@NOSPAMhotbot.com> wrote in message
news:39fa23f3$1@news.devx.com...
>
> Wing Hoe,
>
> I think all of the instances of your classes are instantiated as a single
> class in the VM. It would seem, regardless of location, the VM would see
> these classes as identical classes.
>
Unless they are explicitly being loaded from different packages (which I
doubt but we don't have an example of how they are loaded), the classloader
will always identify one of the classes as "first" in the classpath. So
it's likely that only one of the classes is actually being used.
In my opinion creating two classes with the same name is asking for trouble,
even if you put them into separate packages.
-
Re: consequences of using static variables?
Erm, how do I know that I'm loading it from different packages?
OK, let's put it like this, I'll list the files in the directories first.
D:\project\project1\ *client 1*
PCClient.class
Status.class
PublicChat.class
Login.class
D:\project\project2\ *client 2*
PCClient.class
Status.class
PublicChat.class
Login.class
I run PCClient in their own directories. I assume that these files are in
their own packages, since they're in their own directories, right?
Since I did not explicitly put the "package" keyword in each piece of codes,
therefore, it shouldn't refer to any other classes. What I believe is that
each PCClient will only refer to the classes in the same directory.
The Status.class contains static variables however. But, since they're in
two different directories and they're being accessed by the PCClient class
in the same directory, it shouldn't overwrite each other isn't it? Or am I
assume wrongly?
--
Best Regards,
Wing Hoe
---------------------------------------------------------------
ICQ: 2213281
Email: winghoe@hotmail.com
www: http://pwp.maxis.net.my/winghoe
---------------------------------------------------------------
"Paul Clapham" <pclapham@core-mark.com> wrote in message
news:39fb3ef2@news.devx.com...
>
> Unless they are explicitly being loaded from different packages (which I
> doubt but we don't have an example of how they are loaded), the
classloader
> will always identify one of the classes as "first" in the classpath. So
> it's likely that only one of the classes is actually being used.
>
> In my opinion creating two classes with the same name is asking for
trouble,
> even if you put them into separate packages.
>
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks