OK, so in main you create an instance of Mobile, which calls the Mobile no-args constructor, which calls super(), however in super it calls showDevice(), but which version. Since the instance is of type Mobile it calls the version on Mobile, but since the instance hasn't been created just yet, we are in that process right now the "device" instance variable has not been set. That is why the first call shows "Mobile.showDevice, null".
Basically the instance variable "device" of Mobile is not set till after the return from the super() call to the Mobile's superclass constructor.
Now the other two are printing "Mobile.showDevice, Mobile.showDevice" because it calls the showDevice() method on the instance type which is Mobile, and the "device" instance variable is now set to "Mobile.showDevice"
Does that help?
Mark [ August 27, 2005: Message edited by: Mark Spritzler ]
But again a bit confusion.. as per my understanding ... when instance is created firstly instance variables are created , then they are initialised to default values, then instance initializer expressions and instance initializer blocks are executed and then constructor is called.. Then how come "device" is not initialised
In answer to the previous question: The instance variable Mobile.device has been initialized, to null. But it has not yet had the string "Mobil.device" assigned to it, because that is done at the stage when field initializers are performed. [ August 28, 2005: Message edited by: Barry Gaunt ]
Originally posted by Barry Gaunt: In answer to the previous question: The instance variable Mobile.device has been initialized, to null. But it has not yet had the string "Mobil.device" assigned to it, because that is done at the stage when field initializers are performed.
[ August 28, 2005: Message edited by: Barry Gaunt ]
Yeah, that is what I meant to say instead of "instantiated", "assigned" is a better word to use.
Well further to this discussion i would Appreciate if any one could write down the exact flow of how the variable get instialised when an instance is created. ie example Mobile m =new Mobile()..! Now when this statement is executed what is the the actual flow of intialization ..!And one more point mentioned in the discussion that after the call returns from super only then the instance varaible get their actual value assigned..! Please help me to understand this flow so that I can apply this fundamental to all such questions..!
Thanks a ton in advance..!
Thanks and Regards,<br />Nilesh<br />SCJP 1.4, SCWCD 1.4