0

why isnt it working?

as you can see I am trying to get the input from the constructor , but its not giving me it. how can I make it work? code: import java.util.*; public class Main { public static void main(String[] args) { User u1 = new User("Jerry" , 14); u1.getDetail(); u1.getDetail2(); } } class User <T>{ T t1; T t2; public User(T t1 , T t2) { this.t1 = t1; this.t2 = t2; } public T getDetail(){ return t1; } public T getDetail2(){ return t2; } }

5th Sep 2020, 5:19 PM
Yahel
Yahel - avatar
9 Answers
+ 3
I don't undaerstan what and why that T? See below, i have made some changes it is working: import java.util.*; public class Main { public static void main(String[] args) { User u1 = new User("Jerry" , 14); System.out.println(u1.getDetail()); System.out.println(u1.getDetail2()); } } class User{ String t1; int t2; public User(String t1 , int t2) { this.t1 = t1; this.t2 = t2; } public String getDetail(){ return t1; } public int getDetail2(){ return t2; } }
5th Sep 2020, 6:26 PM
Muhammadamin
Muhammadamin - avatar
+ 1
You are not printing returned results... Make these changes... yahel System.out.println(u1.getDetail()); System.out.println(u1.getDetail2());
5th Sep 2020, 6:41 PM
Jayakrishna 🇮🇳
+ 1
yahel read the comments for reasons... You're welcome.... Note : but actually there no need of generics as you type casting all to Object type only, not to specific type. Practical purpose it's fine.
6th Sep 2020, 5:19 PM
Jayakrishna 🇮🇳
0
Muhammadamin the T is a generic class.
5th Sep 2020, 8:04 PM
Yahel
Yahel - avatar
0
Jayakrishna🇮🇳 I am printing them... look at the main method
5th Sep 2020, 8:05 PM
Yahel
Yahel - avatar
0
yahel your only calling methods, but not catching returned values.. Try with changes of statements I posted above and see difference...
5th Sep 2020, 11:05 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 I did do it, look at lines 7-8
6th Sep 2020, 4:23 AM
Yahel
Yahel - avatar
0
//ohh.. yahel try to understand what is saying by checking in playground.. You can understand it. import java.util.*; public class Main { public static void main(String[] args) { User u1 = new User("Jerry" , 14); u1.getDetail(); u1.getDetail2(); //here you missing return values so no output.. //this statements just calls your methods ,and posibke get returned results. But you need to catch it by storing or printing... //by these ways: String str = (String)u1.getDetail(); System.out.println(str); int num=(Integer)u1.getDetail2(); System.out.println(num); //or just direcltly print by System.out.println(u1.getDetail()); System.out.println(u1.getDetail2()); } } class User <T>{ T t1; T t2; public User(T t1 , T t2) { this.t1 = t1; this.t2 = t2; } public T getDetail(){ return t1; } public T getDetail2(){ return t2; } }
6th Sep 2020, 5:15 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 ohhhh, this whole time I was looking at Muhammadamin s code... I know you need to do that. My bad, Thanks!
6th Sep 2020, 5:17 PM
Yahel
Yahel - avatar