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 3 of 3

Thread: Order of printing

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Order of printing

    Quote Originally Posted by Bhawani View Post
    The output will be :
    x c g
    First the static block will print, then the constructor, and finally the go() will print
    Hi guys, just thought I would jump in here to see if I can get some help with this same question. I understand that the static will print first, and the go() will print last, but what I am really confused on is why the c and the y print in the order they do. They are listed in the following order:
    {System.out.print ("c ");}
    {System.out.print ("y ");}
    but it goes to the second instruction first, hence resulting in the x, y, c, g output.

    I have tried adding additional instructions as follows:
    {System.out.print ("c ");}
    {System.out.print ("y ");}
    {System.out.print ("w ");}
    {System.out.print ("z ");}
    and in every case when it reaches this point, it jumps to the second line first to print whatever is there, follows on with the subsequent lines, only then returning to print the content of the first line before finishing the sequence with the go(). In this example, the output I get is x, y, w, z, c, g.

    I can't understand it and I would really appreciate some help as I have tried to get to grips with it by manipulating the program a dozen ways. I am completely new to Java so go easy on me please! What am I missing/not seeing?

    Thank you in advance!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Help me understand what the print out sequence is please

    If you have code that you are having problems please post all of it, not a few lines.
    Also post the full output from the program.

    See the tutorial: Initializing Fields (The Java™ Tutorials > Learning the Java Language > Classes and Objects)


    Question moved to its own thread.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help me understand what the print out sequence is please

    Ah Norm - would you believe I just figured it out.
    The closing brackets on the initialiser blocks were catching me out. Once you close the first set of brackets, thats the only code that gets passed into the constructor. The next one is just part of the class. So it prints static output, class output, constructor output, method output....
    Got it!

Similar Threads

  1. [SOLVED] Alphabetical order
    By sbjibo in forum What's Wrong With My Code?
    Replies: 12
    Last Post: May 16th, 2012, 03:20 PM
  2. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  3. Threads in some order...
    By aps135 in forum Threads
    Replies: 6
    Last Post: March 11th, 2011, 04:54 PM
  4. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM
  5. Stack Order?
    By TimW in forum AWT / Java Swing
    Replies: 2
    Last Post: September 19th, 2009, 07:33 AM