• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Initialisation order

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roel,

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

Regards
Anne

 
Ranch Hand
Posts: 40
2
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic