Page 1 of 2 12 LastLast
Results 1 to 20 of 25

Thread: Java Blocks??

  1. #1
    abimaran is offline Member
    Join Date
    Nov 2009
    Location
    Colombo, Sri Lanka
    Posts
    22
    Rep Power
    0

    Post Java Blocks??

    Hi!

    Could somebody explain the answer for this?
    How it is happening?

    Java Code:
    class C {
    {System.out.printlnn("Initializer");}
    public static void main(String[] args) {
    System.out.println("Main");
    C obj = new C();
    }}
    the answer is:
    Main
    Initializer

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Main gets called first when your program is run and when you goto create a new C object that's when the default constructor would be called but since you don't have a constructor it just prints out the line of code for the C class.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Is your code something like this?

    Java Code:
    class C {
    
        public C()
        {
            System.out.println("Initializer");
        }
    
        public static void main(String[] args) {
            System.out.println("Main");
            C obj = new C();
        }
    }

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by xcallmejudasx View Post
    Main gets called first when your program is run and when you goto create a new C object that's when the default constructor would be called but since you don't have a constructor it just prints out the line of code for the C class.
    True. But I think it's better if we can pointed him with the complete scenario. I mean with the default constructor an all, as I post in the above post.

  5. #5
    abimaran is offline Member
    Join Date
    Nov 2009
    Location
    Colombo, Sri Lanka
    Posts
    22
    Rep Power
    0

    Default

    No, Eranga!

    Question is correct. No explicit Constructor.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by abimaran View Post
    No, Eranga!

    Question is correct. No explicit Constructor.
    Fine now go through on xcallmejudasxs' post. As he explain the main method is the entry point to the code/application. In Java main method return type is void, means nothing is return to the OS. If you look at a language like C/C++ you can see that the declaration is something like this.

    Java Code:
    int _tmain(int argc, _TCHAR* argv[])
    {
    }
    Return and int to the OS, error code.

    Then in the main method a text line is printed, as you know println() do that. Then create a new object for the class C, with the default constructor in sense. Since no default constructor explicitly define the empty code block,

    Java Code:
    {
            System.out.println("Initializer");
    }
    is executed. There is no order to specify this code block, as you can define any other method or a constructor.

    On the other hand if you have define any other constructor with different signature, you end-up with a compilation error about that default constructor is not define. So you have to change the above code segment as follows.

    Java Code:
    public C()
    {
            System.out.println("Initializer");
    }

  7. #7
    abimaran is offline Member
    Join Date
    Nov 2009
    Location
    Colombo, Sri Lanka
    Posts
    22
    Rep Power
    0

    Default

    Sorry, I couldn't..
    I know the Constructor stuff, but here who call that free block?
    How can be it executed?
    I really don't understand!

    Sorry for the disturbance!

  8. #8
    abimaran is offline Member
    Join Date
    Nov 2009
    Location
    Colombo, Sri Lanka
    Posts
    22
    Rep Power
    0

    Default

    Is it related to static free floating blocks?

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,311
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by abimaran View Post
    Is it related to static free floating blocks?
    They are just initialization blocks and are executed after the super class object has been initialized but before the constructor of your class is exectuted.

    There are also static initialization blocks that are executed just after a class is loaded.

    kind regards,

    Jos

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by JosAH View Post
    There are also static initialization blocks that are executed just after a class is loaded.
    Yes, actually static blocks are executed before start to execute main as well. And also you can define many number of blocks within a class. If multiple blocks are found then they are executing in specified order. Just look at the following code segment, run it and see.

    Java Code:
        static 
        {
            System.out.println("Static Block 1");
        }
    
        public static void main(String[] args) {
            System.out.println("Main");
            C obj = new C();
        }
    
        static
        {
            System.out.println("Static Block 2");
        }
    
        {
            System.out.println("Initializer");
        }

  11. #11
    abimaran is offline Member
    Join Date
    Nov 2009
    Location
    Colombo, Sri Lanka
    Posts
    22
    Rep Power
    0

    Default

    Ok. Thanks to all.

    First static Free blocks, then the main method, after that free blocks(if any) in their specified order! This is the order?

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,311
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Eranga View Post
    [COLOR="DarkGreen"]Yes, actually static blocks are executed before start to execute main as well.
    That is not true; those static initialization blocks are executed just afer a class is loaded. Classes are loaded on demand and that can happen long after the main() method started executing.

    kind regards,

    Jos

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

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

    Default

    Quote Originally Posted by Eranga View Post
    So you said that static block can execute after the main method executed?
    Not in this particular example but you wrote that static blocks are executed before a man method is started; that is not true because a class may get loaded long after main() has started.

    kind regards,

    Jos

  15. #15
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    That is what he said.

    If the class is loaded after the main method has started to run then it's static initializer will run after the main method has started to execute.

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  17. #17
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    Yes, I cannot see where the mislead there.
    The statement "Yes, actually static blocks are executed before start to execute main as well" is the misleading statement because it is wrong.

  18. #18
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Honestly I'm lost here now. Can you explain more why it's wrong. I mean, say I've a static block and a main method. Then how could be execute them, in which order?

    Classes are loaded on demand and that can happen long after the main() method started executing.
    So that means, as an example in previous code, sometimes Main printed on the console before the Static Block1 or else.


  19. #19
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Did you read Jos' reply which started with the words "Not in this particular example ..."?

  20. #20
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Page 1 of 2 12 LastLast

Similar Threads

  1. alternative to static initialization blocks
    By paola in forum New To Java
    Replies: 1
    Last Post: 12-06-2008, 05:51 PM
  2. Replies: 0
    Last Post: 04-09-2008, 06:39 PM
  3. Blocks Language 0.1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 09-22-2007, 11:21 PM
  4. Task Blocks 0.5
    By johnt in forum Java Software
    Replies: 0
    Last Post: 08-08-2007, 08:43 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
  •