• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

page 20 OCA 8 Guide - Java Order Of Initialization

 
Greenhorn
Posts: 25
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

So we say fields and blocks are run first in order. Then the constructor. But doesn't Java always start execution with the main method? The way I read the exercise on page 20 coicidently gave me the right answer but apperently I'm not getting it:
My thought was:
1) I started with the main method since that's where Java starts execution according to page 19. It creates a new instance of Egg
2) So then I jumped to the constructor which assigns the value 5 to the field.
3) I continue down back to the main method where I know print the egg.number as being 5
4( I simply assumed the remainder of the code that assings 3 and then 4 to number, would not be run or be run but would not be printed out as they came after)


What is then the role of the main method if we don't start with it, and instead start with


Thank you
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the complete code from the book Oracle Certified Assosciate Java SE 8 Programmer 1 Study Guide By Jeanne Boyarsky and Scott Selikof


Then authors modified some initial lines to:


Well What they really means for the order is: just after when the new operator is invoked on the class the construction process for an object starts, where the initialization and instance initializer blocks are executed first in the order they are written. so in the first case where the code compiles, the variable name  is initialized before the instance initializer block and thus if we try to access the variable further it will be the value with which it was initialized. however in the next case the instance initializer block will execute first where it is trying to access the variable which is not even declared(or initialized) yet so the compiler will remind us for this(compiler really tries its best to caught some mistakes as early as possible).

For MAIN: yes the code starts right there but when it sees the new is invoked on the constructor, it gets the type of the object it needs to construct and thus before calling the constructor there are some setups applied on the object being constructed like the case we see.

[EDIT]: I will give you some more examples of those setups,  but lets see some more code and try to guess it's output:



Here is the output:


When you give the command(after successful compilation) for running the code: Java Chick.

Here are the things that will happen:

  • Before the invocation of main the class will be loaded(in which main is defined).
  • Loading a class causes the initialization of class variables and static initializer blocks to run in the order they are encountered from top to bottom in class.
  • Please note the static initilaizers(if any) in the super class will be executed before the child class.
  • During execution of one of the static initializer blocks i have created an instance variable inside it, which will cause the execution of instance initializer blocks and instance variable initialization. thereafter the constructor call will execute.
  • Please note: i have called the instance method hello() inside the instance initializer block which is executed before the construcor() which means the dynamic binding of methods with the object is happening before the constructor completes.
  • Notice the value of static variable i, which is printed twice in static initializer block, and think about, why the value 0 and 1 is printed.


  • If you are interested to dig deep in this topic here is the reference to the appropriate section in Java Language Specification: Execution.

    Praveen
     
    praveen kumaar
    Ranch Hand
    Posts: 491
    23
    Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I am sorry, but the original question that the OP is talking about is about this code:


    My previous post covers the context of the question asked by OP.

    Pedro Esgueira wrote:What is then the role of the main method if we don't start with it, and instead start with


    Main is the entry point where actually execution of your code really begins. if you will not have a main method then how can you say where should the code starts executing. java is a OOPS based and saying something like how would it be if we start from some line like you said does not really makes a sense because those lines are part of an object and for executing them we must be able to create objects, but the question comes where can we create those objects, so we need some starting point like a main method(one of the rules made by java). Think about it.

    And I have covered the another similar example(by mistake) from the same page in my previous post.
     
    Pedro Esgueira
    Greenhorn
    Posts: 25
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Pedro Esgueira wrote:

    What is then the role of the main method if we don't start with it, and instead start with


    Thank you



    Hi, what I actually mean with the above is kind of the opposite of what you interpreted from it - and I understand how you could hve interpreted the way you did. Language can be tricky.

    With my "if" I don'I mean to describe an hypothetical sitaution - I'm just describing what seems to me the order of initialization acording to the author's words "If you answered 5, you got it right. Fields and blocks are run first in order, setting number to 3 and then 4. Then.."

    Which then leads me to ask: "what is then the role of the main method" (because, from my interpretation of the authors' words, they seem to not be starting with the main method)

    Thank you for the link to more in-depth initialization. I will take it bit by bit as I go along
     
    praveen kumaar
    Ranch Hand
    Posts: 491
    23
    Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Pedro Esgueira wrote:
    With my "if" I don'I mean to describe an hypothetical sitaution - I'm just describing what seems to me the order of initialization acording to the author's words "If you answered 5, you got it right. Fields and blocks are run first in order, setting number to 3 and then 4. Then.."
    Which then leads me to ask: "what is then the role of the main method" (because, from my interpretation of the authors' words, they seem to not be starting with the main method)...


    Well, it's getting confusing for me. best thing would be to post a complete code as you like to be in class Egg, and separately, show the order of execution by line numbers as per your thinking. that way, perhaps you will get a better help..
     
    Pedro Esgueira
    Greenhorn
    Posts: 25
    2
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi,

    You already helped. I'm quite new to all of this (no background in coding other than following an online course on javascript), so there is just so much I can process in one go.
    Thanks
     
    Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic