+ 5

Help! why 31 why not 13? How?

public static class A{ public String a1; public A(String a1){ this.a1 = a1; } public static void main(String[]args){ A a = new A("1"); c.a = a; a = new A ("3") ; } System.out.println(a.a1); System.out.println(c.a1); }

15th Apr 2019, 3:10 PM
Iftakher Hossain
Iftakher Hossain - avatar
12 odpowiedzi
+ 3
The keyword this is a reference to the current object. In your example you have two Strings a1. One inside your class and one comes from outside. To avoid conflicts you use this to make clear that you mean a1 from your class. this.a1 -> A.a1
20th Apr 2019, 9:16 AM
Denise Roßberg
Denise Roßberg - avatar
+ 2
what about this. can any one explain this? public class A{ public String a1; public A(String a1){ this.a1 = a1; } public static void main(String[]args){ A a = new A("1"); A c = a; a.a1 = new String ("3") ; System.out.print(a.a1); System.out.println(c.a1); } }
20th Apr 2019, 8:03 AM
Iftakher Hossain
Iftakher Hossain - avatar
+ 2
my first question I use this but result is 31.why?Denise Roßberg
20th Apr 2019, 10:14 AM
Iftakher Hossain
Iftakher Hossain - avatar
+ 2
please answer my 2nd zemiak
20th Apr 2019, 10:15 AM
Iftakher Hossain
Iftakher Hossain - avatar
+ 1
A a = new A("1"); --> a.a1 = "1" c.a = a //should it be A c = a? --> c.a1 = "1" a = new A("3") --> a.a1 = "3" First you print a.a1 --> 3 Then you print c.a1 --> 1 --> output: 31
15th Apr 2019, 3:44 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
public class A { // public String a1; public A(String a1) { this.a1 = a1; } public static void main(String[]args) { A a = new A ("1"); // a:1 A c = a; // a:1 c:1 a = new A ("3"); // a:3 c:1 System.out.println(a.a1); // a:3 System.out.println(c.a1); // c:1 } // } /* this is like: a = 1 c = a // = 1 a = 3 print a // 3 print c // 1 output 31 ------- You assign obj a to c and it is same object then you assign new object to a, but in c is original object not new or changed */
15th Apr 2019, 3:51 PM
zemiak
+ 1
System.out.print(a.a1); //33 because i think u write syso print with no (ln )in first line :) System.out.println(c.a1); System.out.println(a.a1); System.out.println(c.a1); if u wirte like that output will 3 3
20th Apr 2019, 5:43 PM
Immortal Crow
Immortal Crow - avatar
+ 1
your second ex return 33, because a and c is the same object, if you change a -> in c you can see new value 3 too this show a and c as is the same object: A a = new A("1"); System.out.println(a+": "+a.a1+"\n"); A c = a; System.out.println(a+": "+a.a1+"\n"+c+": "+c.a1+"\n"); a.a1 = "3"; System.out.println(a+": "+a.a1+"\n"+c+": "+c.a1+"\n"); //output addresses of a c for each steps and it is same address in memory, //after : you can see value of a.a1, c.a1 A@4c75cab9: 1 A@4c75cab9: 1 A@4c75cab9: 1 A@4c75cab9: 3 A@4c75cab9: 3
20th Apr 2019, 6:18 PM
zemiak
+ 1
Thanks a lot zemiak for better understanding.
21st Apr 2019, 1:42 AM
Iftakher Hossain
Iftakher Hossain - avatar
+ 1
what is the main difference between a= new A("3") ; and a.a1=new String("3") ; A/String? why and when i use A or String?
21st Apr 2019, 1:47 AM
Iftakher Hossain
Iftakher Hossain - avatar
+ 1
new keyword is for create object, new A() create object from class A, new String() create object from class String it is same action but with different classes special for String you can write it simpler String s = "" ;
21st Apr 2019, 5:18 AM
zemiak
+ 1
Thanks again zemiak
21st Apr 2019, 6:46 AM
Iftakher Hossain
Iftakher Hossain - avatar