Originally posted by Gurucharan Murudeshwar:
Hello All,
Try out this program :
==================================================
public class TestStatic {
static
{
System.out.println("This is strange !!!");
}
public static void main(String args[]) {
TestStatic ts = new TestStatic();
}
}
==================================================
What would we expect ?
A compilation error ?
But this prints :
+--------------------------------+
| |
| This is strange !!! |
| |
+--------------------------------+
on the screen .
Any explanations ?
We are preparing for the JDK-1.4 Exam.
Cheers,
gurucharan
Originally posted by Gurucharan Murudeshwar:
Yes.
But is this a normal behaviour ?
Cheers.
There is nothing strange about it.
static {} is called a static initializer
A static initializer block executes only once and this happens even before an instance of the class is created.
i,e even before
TestStatic ts = new TestStatic();
Hope this helps,
Sri
SCJP2. Please Indent your code using UBB Code
comp.lang.javascript FAQ: http://jibbering.com/faq/
Originally posted by Gurucharan Murudeshwar:
Hello All,
Generally, we use the static methods by saying
someObj.thatStaticMethod();
But here, the instantiation itself triggers the println line.
SCJP 1.4
SCJP2. Please Indent your code using UBB Code
Generally, we use the static methods by saying
someObj.thatStaticMethod();
But here, the instantiation itself triggers the println line.
Can someone explain the need o such a functionality?
SCJP2. Please Indent your code using UBB Code
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
|