0

Can anyone explain it,esp the last two outputs

public class Test{ char cvalue; float fvalue; Test(char cvalue,float fvalue ){ System.out.println(cvalue+" "+this.cvalue ) ; System.out.println(fvalue +" "+this.fvalue); this.cvalue =cvalue; this.fvalue =fvalue ; } public static void main(String[] args) { Test ref=new Test('a',2.5f); System.out.println(ref.cvalue+" "+ref.fvalue ); Test ref1= new Test('d',9.3f); System.out.println(ref1.cvalue); System.out.println(ref1.fvalue); } }

8th Dec 2019, 3:52 PM
Ajoh Pv
Ajoh Pv - avatar
1 ответ
+ 3
The class Test has two attributes/variables: char cvalue float fvalue The constructor expects two variables: char and float cvalue means the variable from outside, this.cvalue means the attribute from this object Same for fvalue Inside the constructor you print first cvalue + this.cvalue and fvalue and this.fvalue At this time the two attributes have default values char: is not printable float: 0.0 this.cvalue gets value from cvalue same for fvalue In your main method Create an object ref First the constructor is called: You see the printed stuff from the constructor. Then it prints the atttibutes of ref. print statement from constructor: a + default value (which you can't see) 2.5 0.0 print statement from main method: a 2.5 Same for ref1: from constructor: d 9.3 0.0 from main: d 9.3
8th Dec 2019, 4:22 PM
Denise Roßberg
Denise Roßberg - avatar