Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Question ID :957724565630

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this question, the output is 0 and 4. How come the first number displayed is zero? According to the explanation, the variable "i" is not yet initialized, that's why zero is displayed.
My question: Exactly when does an instance variable gets initialized, before constructors are called? or after?
Reference:
class A
{
A() { print(); }
void print() { System.out.println("A"); }
}
class B extends A
{
int i = Math.round(3.5f);
public static void main(String[] args)
{
A a = new B();
a.print();
}
void print() { System.out.println(i); }
}
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marlon,
When an object is created, initialization is done in this order:
(1) Set fields to default initial values (0, false, null)
(2) Call the constructor for the object (but don't execute the body of the constructor yet)
(3) Invoke the superclass's constructor
(4) Initialize fields using initializers and initialization blocks
(5) Execute the body of the constructor
So.. to answer your question.. the instance variables get initialized first at step(1) [ see above] and then get assigned at step 4 (if necessary to do so]
-Sandeep Nachane

------------------
Visit my homepage at
www.tipsmart.com
 
marlon tan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a clarification, we do #4 only after the return from call of superclass constructor, right?
One more thing, what does #4 include:
a. int a = 123; --> we assign 123 to 'a' after #3.
b. int x = functionCall(); --> we assign 'x' with the result of the function call.
c. static initializing blocks --> do we actually execute static init. blocks after #3?
d. non-static initializing blocks --> how about this? after #3 as well?
Thank you very much!
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marlon,
Using Sandeep's list you can add a new one in between 1 and 2.
1.5) Initialize static fields using static initializers and static initialization blocks
Regards,
Manfred.
 
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
out of interest where did you get this question?
 
marlon tan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops... sorry... i forgot to mention its source... I got it from JQ+, but I was not able to keep track of the question number. I know it is "not allowed" to post exam questions without indicating its source...
im sorry about that...
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, marlon,
the reason why 0 got printed instead of 4 is for :
1)Dynamic binding of overriding method during runtime, which is to follow object B in this case
2)int i= 0 at the time while print() method invoked, so 0 got printed!
hope help
Rick
 
Yeast devil! Back to the oven that baked you! And take this tiny ad too:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic