Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,855 questions

51,776 answers

573 users

How to create generic (template) class in Java

1 Answer

0 votes
class Test<T> {
  T a, b;
   
  void setValue(T _a, T _b) {
    this.a = _a;
    this.b = _b;
  }
  Test getValue() {
    return this;
  }
}
 
public class MyClass {
    public static void main(String args[]) {
        Test<Integer> t1 = new Test<Integer>();
        t1.setValue(13, 89);
        System.out.println(t1.a + " " + t1.b);
        
        Test<String> t2 = new Test<String>();
        t2.setValue("java", "c++");
        System.out.println(t2.a + " " + t2.b);
    }
}
 
 
 
 
/*
run:
 
13 89
java c++
 
*/

 



answered Mar 2, 2021 by avibootz
edited Mar 2, 2021 by avibootz

Related questions

1 answer 160 views
2 answers 219 views
1 answer 197 views
1 answer 222 views
1 answer 193 views
1 answer 188 views
...