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.

Results 1 to 5 of 5

Thread: topic related to static final variable and static block !!!!

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Location
    kolkata
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default topic related to static final variable and static block !!!!

    class A{
    static final int a=10;
    static{
    System.out.println("56");
    }
    }
    class B{
    public static void main(String []args)
    {
     System.out.println(A.a);
    }
    }

    The output is 10
    But the explanation is unknown.
    Anyone tell the reason!!!!!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: topic related to static final variable and static block !!!!

    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.

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Location
    kolkata
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: topic related to static final variable and static block !!!!

    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!!!!

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: topic related to static final variable and static block !!!!

    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();
        }
    }

  5. #5
    Junior Member
    Join Date
    Jul 2013
    Location
    kolkata
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: topic related to static final variable and static block !!!!

    Ok..thanks dear...

Similar Threads

  1. topic related to static final variable and static block !!!!
    By Arnab Kundu in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 18th, 2013, 12:12 PM
  2. Replies: 4
    Last Post: November 15th, 2012, 12:09 AM
  3. [SOLVED] non static variable this cant be referenced from a static context
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 06:13 PM