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; } }
9 Respuestas
+ 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;
}
}
+ 1
You are not printing returned results...
Make these changes... yahel
System.out.println(u1.getDetail());
System.out.println(u1.getDetail2());
+ 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.
0
Muhammadamin the T is a generic class.
0
Jayakrishna🇮🇳 I am printing them... look at the main method
0
yahel your only calling methods, but not catching returned values.. Try with changes of statements I posted above and see difference...
0
Jayakrishna🇮🇳 I did do it, look at lines 7-8
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;
}
}
0
Jayakrishna🇮🇳 ohhhh, this whole time I was looking at Muhammadamin s code... I know you need to do that. My bad, Thanks!