J
Jimi Hullegård
The way I understand things, a static block should only run once, when the
class is loaded. But in my test case (se below), it is run several times.
The code:
public class Test
{
private static Test singleton;
private static int hmm = 0;
static
{
System.out.println("--static block");
singleton = Test();
}
private Test()
{
System.out.println("--Creating the Test instance");
System.out.println("--hmm = " + hmm);
hmm++;
}
public static Test getInstance()
{
return singleton;
}
}
public class TestSessionListener implements HttpSessionListener
{
private static Test test;
public TestSessionListener()
{
System.out.println("--new TestSessionListener. this = " + this);
if (test == null)
{
test = Test.getInstance();
}
}
}
All this is in a web application in Apache Tomcat. Now, if I then restart
Apache Tomcat I get the following output:
--new TestSessionListener. this = TestSessionListener@148bd3
--static block
--Creating the Test instance
--hmm = 0
--new TestSessionListener. this = TestSessionListener@1c92535
--static block
--Creating the Test instance
--hmm = 0
--new TestSessionListener. this = TestSessionListener@b0bad7
--static block
--Creating the Test instance
--hmm = 0
--new TestSessionListener. this = TestSessionListener@1126b07
--static block
--Creating the Test instance
--hmm = 0
--new TestSessionListener. this = TestSessionListener@193722c
--static block
--Creating the Test instance
--hmm = 0
--new TestSessionListener. this = TestSessionListener@3e89c3
--static block
--Creating the Test instance
--hmm = 0
My first question is, how come Apache Tomcat create 6 instances of the class
TestSessionListener? That doesn't make any sense to me. But anyway, what
puzzles me even more is the fact that it seems to create 6 instances of the
Test class, and runs the static block 6 times! How is that possible? Is
there 6 java runtime enviroments running at the same time or something? Is
this a bug?
Regards
/Jimi
class is loaded. But in my test case (se below), it is run several times.
The code:
public class Test
{
private static Test singleton;
private static int hmm = 0;
static
{
System.out.println("--static block");
singleton = Test();
}
private Test()
{
System.out.println("--Creating the Test instance");
System.out.println("--hmm = " + hmm);
hmm++;
}
public static Test getInstance()
{
return singleton;
}
}
public class TestSessionListener implements HttpSessionListener
{
private static Test test;
public TestSessionListener()
{
System.out.println("--new TestSessionListener. this = " + this);
if (test == null)
{
test = Test.getInstance();
}
}
}
All this is in a web application in Apache Tomcat. Now, if I then restart
Apache Tomcat I get the following output:
--new TestSessionListener. this = TestSessionListener@148bd3
--static block
--Creating the Test instance
--hmm = 0
--new TestSessionListener. this = TestSessionListener@1c92535
--static block
--Creating the Test instance
--hmm = 0
--new TestSessionListener. this = TestSessionListener@b0bad7
--static block
--Creating the Test instance
--hmm = 0
--new TestSessionListener. this = TestSessionListener@1126b07
--static block
--Creating the Test instance
--hmm = 0
--new TestSessionListener. this = TestSessionListener@193722c
--static block
--Creating the Test instance
--hmm = 0
--new TestSessionListener. this = TestSessionListener@3e89c3
--static block
--Creating the Test instance
--hmm = 0
My first question is, how come Apache Tomcat create 6 instances of the class
TestSessionListener? That doesn't make any sense to me. But anyway, what
puzzles me even more is the fact that it seems to create 6 instances of the
Test class, and runs the static block 6 times! How is that possible? Is
there 6 java runtime enviroments running at the same time or something? Is
this a bug?
Regards
/Jimi