wood burning stoves 2.0
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constrcutors! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constrcutors!" Watch "Constrcutors!" New topic


Sridhar Srinivasan
Ranch Hand

Joined: Nov 07, 2003
Posts: 117
The following code gives the output as
Mobile.showDevice,null Mobile.showDevice,Mobile.device Mobile.showDevice,Mobile.device Can anybody pl explain.Thanks

shishir gupta

Joined: Apr 30, 2004
Posts: 28
here concept of constructor chaining comes in to picture. In main() we are constructing an object of Mobile class. "Mobile n=new Mobile();"

This will invoke the default constructor of Mobile class. Due to constructor chaining super class(Phone) default constructor will be called. It is the object which decides which method to be called. Here object is of Mobile class so showDevice() of Mobile will be called. It will print "Mobile.showDevice, null".
Then Mobile() will be called and it's showDevice() will be called which will print "Mobile.showDevice,Mobile.device". We created an object n of Mobile class which invokes method showDevice() so it will be executed and print "Mobile.showDevice,Mobile.device".

If in case you declare String device as static :

public class Mobile extends Phone {
static String device = "Mobile.device";
void showDevice() {
System.out.print("Mobile.showDevice,"+device+" ");
Mobile() {
public static void main(String[] args) {
Mobile n = new Mobile();

The output will be as follows
Mobile.showDevice,Mobile.device Mobile.showDevice, Mobile.device Mobile.showDevice, Mobile.device
Prithwish Paul Chowdhury

Joined: May 16, 2004
Posts: 3

I have put some efforts to elaborate why the output came as you descibed.
If any constructor gets called the followings steps are followed:

STEP A * Set fields to default initial values (0, false, null)
STEP B * Call the constructor for the object (but don't execute the body of the constructor yet)
STEP C * Invoke the superclass's constructor
STEP D * Initialize fields using initializers and initialization blocks
STEP E * Execute the body of the constructor

Basically ,The instance variables are initialized before the execution of the body of constructor

- The analysis for two lines in main are listed as follows:

Mobile n = new Mobile();

calls Mobile() constructor

Set fields of instance variables of class "Mobile" to defaults (0, false, null);
So Mobile classes String device instance variables gets the value as : device = null

Call the constructor for the object

The first line implicitly calls to super()

Phone() constructor gets called,

-Set fields of instance variables of class Phone to defaults (0, false, null)
-calls the constructor for the object
-initilizes Phone classe's instance variables:=> String device = "Phone.device";
-Calls ALREWADY OVERIDDEN version of showDevice()
-It SUPPOSE To PRINT "Mobile.showDevice,"+Mobile instance variable "device" => "Mobile.showDevice,null"

The second line calls showDevice() which calls OVERIDDEN version of showDevice() and prints

Here the instance of Mobile : n calls the method showDevice() and prints

One may verify the concepts described above by changes at @(A), @(B) @(C):
The output is listed at the end of the code.


device phone->Phone.device [Changes in @(A)]
device Mobile phone Mobile.device[Changes in @(B)]
device phone->Phone.device[Changes in @(C)]

Please feel free to reiterate any of the points missed in this regards.

Bert Bates

Joined: Oct 14, 2002
Posts: 8898
Constructors! Are so exciting!! Let's all SHOUT!!! about them !!!

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Sandeep Advani
Ranch Hand

Joined: Mar 11, 2004
Posts: 78
Well explained Prithwish. You even cleared my concepts for constructor chaining.

I agree. Here's the link: http://aspose.com/file-tools
subject: Constrcutors!
It's not a secret anymore!