0
someone explain why x equals 0?
11 Answers
+ 1
If you do not remove it then it will be a local variable and it's scope will be limited to the constructor block.
But if you remove int then the instance variable x which is declared outside the constructor will be modified.
+ 2
Because x is an instance variable and they have default values, it is 0 for int type.
Edit: To get 5 as output, kindly remove the "int" inside both constructors.
c=5; & x=c;
+ 1
Avinesh finally figured out the machinenics,pretty much helpful!
0
Avinesh but the constructor of B got called,why x is still 0?
0
Avinesh so the value of x didnt be accessed,thank you
0
Have a look at this.
https://code.sololearn.com/cdEoIW4E9WRd/?ref=app
0
Avinesh why int should be removed?thanks
0
jcx2010 good to know that👍
0
You are creating new local variable
Just remove int from constructors
0
Forgot "this" in constructor