Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in output(constructor related)

 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
Chrome Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arjun, Did you solve this?
 
amit mandal
Ranch Hand
Posts: 46
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@abhiriman-no, still didn't get through
@amit see this code

it prints:1
but that wasn't my question.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my question was on post 3.
thanks
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Abhirimaran
in this code why is the super constructor running even without being called?

output1
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@abimaran and amit -why above code prints 1?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes Abimaran finally,i got it. thanks
sorry for misquoting your name.
thanks all for giving your precious time.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic