How to create a Java class, similar to a C++ template class?
61,176
Solution 1
class SomeClass<T> {
private T data;
public SomeClass() {
}
public void set(T data_) {
data = data_;
}
}
You probably also want to make the class itself public, but that's pretty much the literal translation into Java.
There are other differences between C++ templates and Java generics, but none of those are issues for your example.
Solution 2
You use "generics" to do this in Java:
public class SomeClass<T> {
private T data;
public SomeClass() {
}
public void set(T data) {
this.data = data;
}
};
Wikipedia has a good description of generics in Java.
Solution 3
/**
* This class is designed to act like C#'s properties.
*/
public class Property<T>
{
private T value;
/**
* By default, stores the value passed in.
* This action can be overridden.
* @param _value
*/
public void set (T _value)
{
value = _value;
}
/**
* By default, returns the stored value.
* This action can be overridden.
* @return
*/
public T get()
{
return value;
}
}
Solution 4
public class GenericClass<T> {
private T data;
public GenericClass() {}
public GenericClass(T t) {
this.data = t;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
// usage
public static void main(String[] args) {
GenericClass<Integer> gci = new GenericClass<Integer>(new Integer(5));
System.out.println(gci.getData()); // print 5;
GenericClass<String> gcs = new GenericClass<String>();
gcs.setData("abc");
System.out.println(gcs.getData()); // print abc;
}
}
Author by
sivabudh
First, I'm a dreamer who engineers and innovates. Second, I CEO at CODIUM and 1000C startups.
Updated on July 09, 2022Comments
-
sivabudh almost 2 years
How do I write an equivalent of this in Java?
// C++ Code template< class T > class SomeClass { private: T data; public: SomeClass() { } void set(T data_) { data = data_; } };
-
Laurence Gonsalves over 14 yearsActually, one difference, which isn't a generic/template thing but a more fundamental difference between C++ and Java, is that the set method in C++ copies the supplied object, while the Java version just copies the reference.
-
sivabudh over 14 yearsWow, and the cool thing about "generics" is that I can even express something like public class SomeClass<T extends OtherClass>! I dont think that ability is present in C++. So cool!
-
Admin over 14 yearsShaChris23: the prevalent idiom in the C++ stdlib is to use duck typing: "if all the attempted uses work with the object, it has the interface expected" (std::reverse_iterator being a good, clear example), and this is similar to Python and [my understanding of] Go. It is still sometimes useful to make those restrictions yourself (or at least people ask for it :P), and you can use boost::enable_if, static asserts (with type traits to detect inheritance in this case), and similar for that. Concepts were supposed to help address this, but that's another story.
-
aaronman over 10 years@ShaChris23
is_base_of
does exactly that, not to mention C++ templates have countless abilities that java generics don't, they are turing complete for god sakes, IMO one of the best features of C++ -
Laurence Gonsalves over 10 years@aaronman
is_base_of
was added in C++11, but ShaChris23's comment was from 2009. In other words, it wasn't part of (standard) C++ when he made that comment.