+ 1

Explanation for output of this java code..

https://code.sololearn.com/cjvc8m5n5Eno/?ref=app

7th Jun 2020, 3:00 AM
Harsh Vyas
Harsh Vyas - avatar
4 Respostas
+ 2
Harsh Vyas because you are calling c2.sum(5) method code c1=new code(); code c2=new code(); c1.harsh="blue"; ==> c2.sum(5); System.out.println(c1.harsh); System.out.println(c2.sum(5));
7th Jun 2020, 11:07 AM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 2
It don't prints 5 unless you call it through System.out.println() method.
7th Jun 2020, 11:11 AM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 1
You are retrieveing feild and method of code class from code1 by creating object of code class. However you don't need two different objects instead you can we acess both of the variable and method from one single object. code c1=new code(); c1.harsh="blue"; System.out.println(c1.harsh); System.out.println(c1.sum(5));
7th Jun 2020, 4:20 AM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
0
But AMOGHA. A. K. Why this sop(c1.harsh); is printing harsh is blue instead of blue
7th Jun 2020, 5:41 AM
Harsh Vyas
Harsh Vyas - avatar