• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with static counter  RSS feed

 
Adrian Perry
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Doubt regarding the use of keyword static.

I have made 3 classes Static1, StaticEg and StaticTesting. Static1 has a static counter count which is incremeted each time it is accessed. This count is incremented by 2 classes StaticEg and StaticTesting.

My question is why does the counter gets reseted everytime.

The code is given below:

first class
package staticdemo;
class Static1
{
private static int count;

public static int getCount()
{
System.out.println("in static class "+count);
return count;
}
public static void incrementCount()
{
count++;
}
};

Second class
package staticdemo;
public class StaticTesting
{
public static void main(String[] args)
{
System.out.println("from statictesting "+Static1.getCount());
for (int i=0;i<3 ;i++ )
{
Static1.incrementCount();
}
System.out.println("from statictesting "+Static1.getCount());
}
}

Third Class

package staticdemo;
public class StaticEg
{
public static void main(String[] args)
{
System.out.println("from staticeg "+Static1.getCount());
for (int i=0;i<5 ;i++ )
{
Static1.incrementCount();
}
System.out.println("from staticeg "+Static1.getCount());
}
}

Hoping u would solve my query.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a basic misconception that is confusing static with "persistent". A process is a running program. Every time you run a Java program, by enetring "java mypackage.MyClass" on the command line, for example, you are launching a fresh process that runs a new instance of the JVM to execute your program. The lifetime of a static variable is confined to the lifetime of that JVM. When you program stops, the static variable disappears without a trace. If you run that program again, or another program, the new process will be unaware that the previous process had run and created static variables. if you want data to be persistent, you have to use a database or use file I/O, etc...
 
Adrian Perry
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jeff,

Wasn't aware that new instance of jvm is created. Do not recall reading it ever that a new instance of jvm is created whenever i do java packagename.name

I am trying to use this static class in a servlet, whereby i can count how many thread of servlets service method are formed. Everytime a new thread of the thread is created the counter is incremented.

But never heard a new instance of jvm is created....but it may be the only explaination beacuse the counter, is certainly reseting everytime.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Adrian Perry:

Wasn't aware that new instance of jvm is created. Do not recall reading it ever that a new instance of jvm is created whenever i do java packagename.name


That's the way it is. Perhaps you have some misconception about Java or standalone applications in general. I mean, if a program stops running, that's it, isn't it?

Originally posted by Adrian Perry:

I am trying to use this static class in a servlet, whereby i can count how many thread of servlets service method are formed. Everytime a new thread of the thread is created the counter is incremented.


Whoah, howdy-doo, you just completely changed the context of your question. There's a huge difference between separate executions of standalone applications and separate calls to the same servlet instance! Perhaps the best way to proceed is to step back and state: what are you trying to do, what is your goal? There was no mention of servlets in your original post!! Also, talk of "Everytime a new thread of the thread is created" (no idea what that means) makes me nervous. Servlets usually do not create new threads. You may be misunderstanding the servlet container model that web apps use.

Again, asking a specific question about statics may not be the best way to solve your problem; stating your problem -- what your goal is, what you are trying to do -- may be more helpful.
[ February 02, 2006: Message edited by: Jeff Albertson ]
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can not separately run your codes (StaticEg and StaticTesting), they do not use the same JVM machine. You have separate Static1 object. But I do not think you will have when you use Static1.java in your servlet leave because they are running in the same JVM machine.

You can try the following codes. Here is what I made change to your code:
StaticEg.java file

StaticTesting.java file

and added new test file:
StaticTest.java
 
Adrian Perry
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Sorry for the late reply. I was a bit preoccupied for a couple of days. Thanks Jeff for explaining to me that a new instance of jvm is created and thanks wise ou for explaining stuff to me with example. I appreciate guys.

Adrian.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!