Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By gimbal2

Thread: Explanation required

  1. #1
    marcvaf is offline Member
    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0

    Default Explanation required

    Hi i am new to java and studying for the programmer 1 exam
    I have ran this program and i am still not sure how it printing out the output, could someone explain this?
    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 "); }
    }


    Answer is x y c g

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Explanation required

    Google keyword: initializer blocks.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,018
    Rep Power
    6

    Default Re: Explanation required

    Quote Originally Posted by KevinWorkman View Post
    Google keyword: initializer blocks.
    Correction: java initializer blocks. Need to put it into context, or else poor Google will not know what you want specifically :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    marcvaf is offline Member
    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0

    Default Re: Explanation required

    many thanks for that! much appreciated!

  5. #5
    marcvaf is offline Member
    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0

    Default Re: Explanation required

    Just a quick question i understand that the X would be printed first but i am not sure why the Y would be printed before the C? any ideas?

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Explanation required

    Quote Originally Posted by marcvaf View Post
    Just a quick question i understand that the X would be printed first but i am not sure why the Y would be printed before the C? any ideas?
    That 'y' is printed in the initializer block and initializer blocks run before the code in the constructor runs.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    marcvaf is offline Member
    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0

    Default Re: Explanation required

    thanks makes sense now :)

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Explanation required

    Quote Originally Posted by gimbal2 View Post
    Correction: java initializer blocks. Need to put it into context, or else poor Google will not know what you want specifically :)
    Googling just "initializer blocks" returns a bunch of results for me, including the Java tutorials. Although that might be google being creepy and knowing I want Java stuff all the time...
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,018
    Rep Power
    6

    Default Re: Explanation required

    Quote Originally Posted by KevinWorkman View Post
    Googling just "initializer blocks" returns a bunch of results for me, including the Java tutorials. Although that might be google being creepy and knowing I want Java stuff all the time...
    Exactly right, Google is returning you personalized search results. We can't assume it will do the same for everyone right off the bat - in fact it won't.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Explanation required

    Quote Originally Posted by gimbal2 View Post
    Exactly right, Google is returning you personalized search results. We can't assume it will do the same for everyone right off the bat - in fact it won't.
    Out of curiosity I just did a search of "initializer blocks" on a neutral computer, new browser, not logged in to google, and I got these results, in this order, all of which are about Java. Do you get something else?

    Initializing Fields (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Static block vs. initializer block in Java? - Stack Overflow
    java - What is an initialization block? - Stack Overflow
    java - Static Initialization Blocks - Stack Overflow
    java - Instance initialization block and subclasses - Stack Overflow
    In what order do static initializer blocks in Java run? - Stack Overflow
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,018
    Rep Power
    6

    Default Re: Explanation required

    I'm a bad example because I will most certainly get results relating to Java; that's the only language I use nowadays. You miss my point; we cannot make the assumption to advise to use a google search without the context, the "other party" might just get whole different results based on previous searches that are similar but related to a different context. But this off-topic discussion has now dragged on for so long that I start to feel awkward about it, I'd much rather drop it because this starts to look like I'm lecturing while I was only making a little side remark :/ This should have been in PMs IMO.

    I compromise: I accept your test effort and results and agree that in this specific case, the OP would very likely have gotten proper results (and so I spoke out of turn). But adding that little word Java still pretty much guarantees it.
    Last edited by gimbal2; 01-14-2014 at 03:48 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Explanation required

    Quote Originally Posted by gimbal2 View Post
    I'm a bad example because I will most certainly get results relating to Java; that's the only language I use nowadays. You miss my point; we cannot make the assumption to advise to use a google search without the context, the "other party" might just get whole different results based on previous searches that are similar but related to a different context. But this off-topic discussion has now dragged on for so long that I start to feel awkward about it, I'd much rather drop it because this starts to look like I'm lecturing while I was only making a little side remark :/ This should have been in PMs IMO.

    I compromise: I accept your test effort and results and agree that in this specific case, the OP would very likely have gotten proper results (and so I spoke out of turn). But adding that little word Java still pretty much guarantees it.
    Agreed, but I think initializer blocks are already Java-centric enough that the results will already be pretty spot-on. Anyway, consider it dropped. :p
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,018
    Rep Power
    6

    Default Re: Explanation required

    I'm sorry but I can't, I need a hug now.
    KevinWorkman likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Explanation required

    Quote Originally Posted by gimbal2 View Post
    IBut this off-topic discussion has now dragged on for so long that I start to feel awkward about it, I'd much rather drop it because this starts to look like I'm lecturing while I was only making a little side remark :/ This should have been in PMs IMO.
    No, we want the whole story, references and all; as the old Roman said: "Introductio, Narratio, Conclusio"; you may thank me in your acknowledgements.

    kindest regards,

    Jos (ahem)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 11-25-2013, 10:48 PM
  2. Replies: 1
    Last Post: 12-03-2011, 01:01 PM
  3. need explanation
    By marie in forum New To Java
    Replies: 2
    Last Post: 03-21-2010, 03:35 PM
  4. explanation of this loop?
    By glopez09 in forum New To Java
    Replies: 4
    Last Post: 11-15-2009, 02:36 AM
  5. need a little explanation
    By cew27 in forum New To Java
    Replies: 7
    Last Post: 12-13-2007, 11:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •