Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initialisation order

 
Anne Johaben
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To begin, I apologize about my English, it's not my mother language...sorry for all mistakes...

I'm starting to read the OCA Java SE8 Programmer 1 and I have a question about the initialization order.

Could you please have look at this code :



1 - If the "number" declaration is in the comments (//private int number=3;), the code doesn't compile.
There is an error at the line : "number=4;" because "number" is undeclared.

2 - After removing the comments, when I execute this code, the result is "number :3".


My question is :
- when the statement "{number=4;}" is executed ?

If it is executed in first, I think the program should have to crash because the variable is not declared.
If it is executed after the declaration/affectation statement, the result displays should be "number :4"...

It seems this statement is never executed. I'm right ? Is it possible ?

Thank you for your help. Regards
Anne
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anne Johaben,

First of all, a warm welcome to CodeRanch!

Anne Johaben wrote:To begin, I apologize about my English, it's not my mother language...sorry for all mistakes...

No problem! It's an international forum so you are definitely not the only one who isn't a non-native English speaker (e.g. I'm another one as my mother language is Dutch )

Anne Johaben wrote:1 - If the "number" declaration is in the comments (//private int number=3;), the code doesn't compile.
There is an error at the line : "number=4;" because "number" is undeclared.

I think that's pretty obvious: no instance variable number declared in the Egg class, so you can't initialize number as it doesn't exist.

Anne Johaben wrote:- when the statement "{number=4;}" is executed ?

It's kind of tricky. The statement is definitely executed, but you don't see the result because it's immediately overridden with 3. It's like you would execute this codeIn fact, when you compile your code snippet, you get the above code snippet (of course the main() method will be present as well ).

Now let's try to switch the declaration of number with the instance initializer block and see what happens.Output:
number (declaration): 3
number (init block): 4
number (main): 4


Hope it helps!
Kind regards,
Roel
 
Anne Johaben
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel,

A big thanks for your response, it's really clear and, yes, it helps

Regards
Anne

 
Kaleem Anwar
Ranch Hand
Posts: 40
2
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:

Now let's try to switch the declaration of number with the instance initializer block and see what happens.Output:
number (declaration): 3
number (init block): 4
number (main): 4


Hope it helps!
Kind regards,
Roel


Hi Roel,

Could you please clear one doubt, my understanding of above code is this.:

1. Execution starts from main
2. Egg class constructor called.
3. Egg class constructor called the super class constructor.
4. Right after super class constructor completion, initialization block start execution in order they are defined.
5. After this, egg class constructor resumes and completes.
6. Execution back to the main method.

At which point the declaration of number is executed, I mean when will it get the default value of zero(0) and when 3?

Thanks.

Regards,
Kaleem
 
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
Kaleem Anwar wrote:Could you please clear one doubt, my understanding of above code is this.:

You could easily find out by adding a superclass, some constructors and a few System.out.println statements. If you want to have a full overview of the execution flow of the code have a look at this post: it has static initializer blocks, constructors and instance initializer blocks in both a superclass and a subclass. And the output of the code snippet in that post confirms your understanding

Kaleem Anwar wrote:At which point the declaration of number is executed, I mean when will it get the default value of zero(0) and when 3?

That's a lot harder to determine. When the super constructor is executing, the number instance variable will already exist and is initialized with the default value 0. When the Egg class constructor resumes, it will be initialized with the explicit values. I could create a code snippet to illustrate but it's much easier to share this link. It is a topic about exactly this question and has some excellent posts with great code examples. Definitely worth reading (from top to bottom)!

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic