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

Thread: Help me understand what the print out sequence is please

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Location
    Auckland
    Posts
    9
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help me understand what the print out sequence is please

    public class Sequence { 
    Sequence() { System.out.print("c "); } 
    { System.out.print("y "); } 
    public static void main(String[] args) { 
    new Sequence().go(); 
    } 
    void go() { System.out.print("g "); } 
    static { System.out.print("x "); } 
    }

    so that's the code and the print out sequence is xycg .....what is it that is driving the sequence output to do it in this order?

    thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    Format the code so it is readable then put comments next to each println in the order that they are executed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Location
    Auckland
    Posts
    9
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

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

    that is a sample question on a java certification test.....are you saying that this code is not written out properly even though it runs correctly?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    What I mean is that the poor, cramped formatting of the code makes it hard to read. Properly formatted code is much easier to read and understand.
    The added comments will help you read the code to more easily see what order the statements are executed in.

    Read this about initialization:
    http://docs.oracle.com/javase/tutori...O/initial.html
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Location
    Auckland
    Posts
    9
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

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

    The funny thing is, Norm, I got that sample question from Oracle lol. That's a sample pull straight from their website for SE7 programmer exam lol So if that's the way they're going to formulate their questions....they're just making it harder for the person taking the tests.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    Whoever wrote it doesn't change it from being poorly formatted.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    My Mood
    Nerdy
    Thanks
    0
    Thanked 2 Times in 2 Posts

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

    The output will be :
    x c g
    First the static block will print, then the constructor, and finally the go() will print

  8. The Following User Says Thank You to Bhawani For This Useful Post:

    DerrickMartin (February 19th, 2013)

Similar Threads

  1. Replies: 1
    Last Post: December 3rd, 2012, 02:35 PM
  2. Sequence Classification
    By SilentNite17 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 1st, 2012, 08:25 AM
  3. xfa.host.print: print a page + some page range.
    By gammaman in forum Totally Off Topic
    Replies: 2
    Last Post: May 10th, 2012, 08:07 AM
  4. Sequence
    By r_james14 in forum Java Theory & Questions
    Replies: 2
    Last Post: April 8th, 2012, 10:05 PM
  5. sequence of integers
    By einjhelclint12 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 1st, 2012, 07:49 PM