aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in output(constructor related) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in output(constructor related)" Watch "doubt in output(constructor related)" New topic
Author

doubt in output(constructor related)

Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432


output:1234
but i was thinking it is 11234
coz no arg constructor of X class has been called twice
1. when we instantiate new Main(5), as it will called the super class constructor(no arg.) first.
2. when overloaded constructor of X class being called in line 3 as a result of line 4
tell me where i am wrong?


SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
why do you think so ? The constructor of super class will be run only once. Can you tell me in detail what are you thinking.


SCJP 1.6 96%
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

thanks for reply.
i think X() will run twice.
1. firstly super class constructor(no arg.) i.e. X() will run when we write new Main(5);.
2. at line 2 ,when we are calling this();
isn't it?
Sumit Khurana
Ranch Hand

Joined: Sep 19, 2010
Posts: 68

No dude.
You are thinking in a wrong way.

when we instantiate main(5) it calls the Main(int y) and in this there can be only this() or super().

here is this(), so it will call the other constructor..
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Sumit Khurana wrote:No dude.
You are thinking in a wrong way.
when we instantiate main(5) it calls the Main(int y) and in this there can be only this() or super().

super class (no arg) constructor X() will first run when we instantiate subclass Main.
if we remove super(6); in line 3.
output:134

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Arjun, Did you solve this?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
amit mandal
Ranch Hand

Joined: Aug 13, 2010
Posts: 46

when new Main(5) is called, it will go to main(int y). There it will encounter with this(), control will go to Main() and there it will encounter with super(6) thus calling the super constructor X(int x). Then it will go to X() after encountering this(). Then it will print 1,then it returns to line 2,printing 2. then it comes back to line 3,printing 3 and finally to line 4,printing 4.
X() is called only once. Thus the output is 1234. Hopes it helps.


ocpjp 6.0 certified.
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

@abhiriman-no, still didn't get through
@amit see this code

it prints:1
but that wasn't my question.
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

my question was on post 3.
thanks
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Arjun Srivastava wrote:@abhiriman-no, still didn't get through

Who is this? Whatever, let's solve this.
Arjun Srivastava wrote:thanks for reply.
i think X() will run twice.
1. firstly super class constructor(no arg.) i.e. X() will run when we write new Main(5);.
2. at line 2 ,when we are calling this();
isn't it?

No.


1) You call main(5) -> invokes the constructor on the line 8.
2) There, you've called this() -> invokes the constructor on the line 7.
3) There, you've called super(6) -> invokes the constructor on the line 4.
4) On 4th line, you've called this() -> invokes the constructor on the line3.
5) There you've printed 1.

And rest, in the reverse order, every construvtor finished their jobs. So, that's the output!

Does this make sense?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Arjun Srivastava wrote:
2. at line 2 ,when we are calling this();
isn't it?


Why do you think in this way? Where did you get this routine?
amit mandal
Ranch Hand

Joined: Aug 13, 2010
Posts: 46

@Abhirimaran
in this code why is the super constructor running even without being called?

output1
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

@abimaran and amit -why above code prints 1?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

amit mandal wrote:@Abhirimaran
in this code why is the super constructor running even without being called?


I'm searching this person so many times here!
The compiler implicitly put this for you!
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Abimaran Kugathasan wrote:
The compiler implicitly put this for you!

why compiler doesn't implicitly puts it in code given in my first post?
amit mandal
Ranch Hand

Joined: Aug 13, 2010
Posts: 46

Abimaran Kugathasan wrote:
amit mandal wrote:@Abhirimaran
in this code why is the super constructor running even without being called?


I'm searching this person so many times here!
The compiler implicitly put this for you!


hahaha sorry for misquoting your name :P. I saw Arjun using it and I used it without checking Sorry
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Arjun Srivastava wrote:
why compiler doesn't implicitly puts it in code given in my first post?

If you don't specify it, the compiler will automatically insert it for you.

And, could you please tell us, in which point you've problem in this stuff? So, we can clear it!
amit mandal
Ranch Hand

Joined: Aug 13, 2010
Posts: 46

Abimaran Kugathasan wrote:
Arjun Srivastava wrote:
why compiler doesn't implicitly puts it in code given in my first post?

If you don't specify it, the compiler will automatically insert it for you.

And, could you please tell us, in which point you've problem in this stuff? So, we can clear it!


Atleast my doubt is clear now. In the first code it wasnt called implicitly as it was already called explicitly in the code. But in the last code it wasnt called at all thus the complier called it implicitly. Am i right Abimaran?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

amit mandal wrote:
Atleast my doubt is clear now. In the first code it wasnt called implicitly as it was already called explicitly in the code. But in the last code it wasnt called at all thus the complier called it implicitly. Am i right Abimaran?

Seems to be right, but, which is the first code, and which is the last code? So many codes are used here.
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

first code

output:1234

last code

output:1
In the first code X() wasnt called implicitly as it was already called explicitly in the code.
But in the last code X() wasnt called at all thus the complier called it implicitly.
does this makes sense?



ok this is modified code of first code(here we have not called X() explicitly),then why compiler doesn't put X() implicitly?

output:234
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

The first statement within a Constructor should be this() or super(). If you don't specify anyone, then the Constructor will insert it for you! But,if the super class has a argumented constructor, then you are in trouble! Try it!

Did you get it?
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Abimaran Kugathasan wrote:The first statement within a Constructor should be this() or super(). If you don't specify anyone, then the Constructor will insert it for you! But,if the super class has a argumented constructor, then you are in trouble! Try it!
Did you get it?

sorry i have edited my last post ,added one more code,you have already seen it?
amit mandal
Ranch Hand

Joined: Aug 13, 2010
Posts: 46

Abimaran Kugathasan wrote:The first statement within a Constructor should be this() or super(). If you don't specify anyone, then the Constructor will insert it for you! But,if the super class has a argumented constructor, then you are in trouble! Try it!

Did you get it?


You are right. With argument-ed super constructor the compilations fails when its not called explicitly.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Arjun Srivastava wrote:
ok this is modified code of first code(here we have not called X() explicitly),then why compiler doesn't put X() implicitly?

output:234

The compiler doesn't insert X(), but insert super(). Here, within the X(int x) Constructor, you haven't call this(), so X() Constructor won't be called, and the compiler insert super() within the X(int x) Constructor, and it calls the no-arg Constructor of the Object class.

Now, is it OK?
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

yes Abimaran finally,i got it. thanks
sorry for misquoting your name.
thanks all for giving your precious time.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Arjun Srivastava wrote:yes Abimaran finally,i got it. thanks
sorry for misquoting your name.
thanks all for giving your precious time.

That's great! You are Welcome.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: doubt in output(constructor related)