• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Order of Initialization on page 19 (Java OCA 8 Programmer I Study Guide)

 
Raghavendra Desoju
Ranch Hand
Posts: 95
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel information given about "Order of Initialization" is incomplete. Here is my complete program:

Here is my program:
=============


Here is the output:
============
In block before -->3
In block after -->4
before init - in constructor -->4
in constructor
Value: 5


There is no difference if order is changed. see below:-



Output:-
In block before -->3
In block after -->4
before init - in constructor -->4
in constructor
Value: 5



One more observation:-




Output:-

OrderCheck.java:12: error: illegal forward reference
{number = 4;System.out.println("In block after -->" + number);}
^
1 error
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raghavendra Desoju,

First of all, a warm welcome to CodeRanch!

Raghavendra Desoju wrote:I feel information given about "Order of Initialization" is incomplete.

I can't see anything wrong with the order of initialization mentioned on page 19. And it's incomplete (e.g. nothing is mentioned about static initializer blocks), but the study guide clearly mentions other rules will be added in chapter 4 and 5.

The rules mentioned on page 19 are correct though:
  • first all field and instance initializer blocks are executed in the order they appear in the source file
  • the constructor runs after all field and instance initializer blocks have run


  • If you want to see some difference, then you have to use different initializer blocks in your code. Let's have a look at your first example which I changed a little bitOutput:
    In block1 --> 3
    In block2 --> 4
    in ctr1 --> 4
    in ctr2 --> 5
    number = 5


    Now let's change the order of the instance initializer blocks and see what happensOutput:
    In block2 --> 3
    In block1 --> 3
    in ctr1 --> 3
    in ctr2 --> 5
    number = 5


    So this output is completely different than the one of the first code snippet. And it illustrates perfectly the rules mentioned in the study guide!

    Hope it helps!
    Kind regards,
    Roel

    PS1. Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers. I've gone ahead and added the code tags for you. See how much easier the code is to read?
    PS2. Please mention the page number when you are posting/quoting something from a study guide, it will help people to find the text and/or code snippet you are talking about.
     
    Raghavendra Desoju
    Ranch Hand
    Posts: 95
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Roel,
    Thanks a lot for your reply. One more question:-
    What is the difference between below two code snippets? Why would compiler throw an exception for block 2 below?

    ========1==========

    ========2===========

    ===================


    Please clarify.

    Thanks, Raghu
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Raghavendra Desoju wrote:Hi Reol,

    My name is Roel, not Reol Eye for detail is very important for a developer and certainly if you are taking the OCA exam where details could make the difference between compilation success or compilation failure.

    Raghavendra Desoju wrote:What is the difference between below two code snippets? Why would compiler throw an exception for block 2 below?

    Good question! The difference is caused by what's inside the instance initializer block: in the 1st code snippet it's an initialization of instance variable number, in the 2nd code snippet it's printing the instance variable number. But you are printing the number before it gets initialized. And you can't reference/access a field before it is defined. The restrictions on the use of fields during initialization are defined in the (sometimes very hard to read and understand) JLS, so read at your own risk

    Hope it helps!
    Kind regards,
    Roel
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I see you have edited your previous post to add code tags as requested in one my previous posts. Well done! Have a cow for doing that and for asking a very good question!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic