Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Please don't double post. The answer to this question is staring you in the face, and I'm not sure why you can't see it. Stare at this line:
System.out.println(A.a);
until you can answer the question, "What does it do?"
Since you have seen what it does, the answer should come to you quickly.
Since dear....
My problem'sanswer is known to me...
But explanation is not clear..
Bcz every time a static field will execute immediately after the static block..here not happen...it was my question...
Ok...dear...i find out myself with staring eyes!!!!
Your latest comments make me think I didn't understand your question.
Compare the results of your example to mine below and see if that clears up your confusion. The key is to know when the static initialization block (or "static block" as you called it) will be executed.
class A { static final int a=10; static { System.out.println("56"); } public A() { System.out.println( "In A" ); } } public class StaticTest { public static void main(String[] args) { System.out.println( A.a ); new A(); } }
Ok..thanks dear...