I asked  over 10 years ago, about C#, and got a good answer there. Now I'm trying to do the same thing in Java.  Say I have an abstract class Animal. I want a static method create() in that class, that will return a value of the type of whatever the subclass is. I managed this in C# using the following structure:  public class Animal&lt;T&gt; where T : Animal&lt;T&gt;, new() {     public static T Create()     {         var t = new T();         // do generic stuff on the new T instance         return t;     } }  public class Dog : Animal&lt;Dog&gt; {  }  //usage: var dog = Dog.Create();   This worked a charm in C#. But now I'm trying to replicate this structure in Java, and hitting a wall. One big wall is that it seems that . So this would seem to be a bit of a showstopper. But I'm an optimistic kind of guy, and maybe some Java expert out there has come up with a totally out of the box pattern for achieving more or less what I'm trying to do here?
Is it possible to create an instance of a generic type in Java? I'm thinking based on what I've seen that the answer is no (due to type erasure), but I'd be interested if anyone can see something I'm missing:  class SomeContainer&lt;E&gt; {     E createContents()     {         return what???     } }   EDIT: It turns out that  could be used to resolve my issue, but it requires a lot of reflection-based code, as some of the answers below have indicated.  I'll leave this open for a little while to see if anyone comes up with anything dramatically different than Ian Robertson's .
How can I add a folder to the launcher for quick access to it. In this particular case I wish to drag it to the launcher. How do I change my username? Some time ago, when I installed Ubuntu, I chose a rather stupid username for my account that I do not want to use anymore. How do I change this (including the name of my home directory, and the name in the terminal) without losing settings for applications? How do I keep permissions and my keys for various authentification (e.g. email, SSH, GPG and more)? What settings could possibly get lost if I changed my username? Dwellers return automatically after finding a low amount of items in Wasteland How to use unicode characters in Windows command line? Is it possible to write some C or C++ code and compile to binaries, then use those binaries with php? Is it also possible to write a php library using C and C++ ?  If so, please tell how can I do so? We know that $\displaystyle\zeta(2)=\sum\limits_{n=1}^{\infty} \frac{1}{n^2} = \frac{\pi^2}{6}$ and it converges.        Does there exists a bijective map $f:\mathbb{N} \to \mathbb{N}$ such that the sum $$\sum\limits_{n=1}^{\infty} \frac{f(n)}{n^2}$$ converges.      If our $s=2$ was not fixed, then can we have a function such that $\displaystyle \zeta(s)=\sum\limits_{n=1}^{\infty} \frac{f(n)}{n^s}$ converges How to encrypt external devices using a cross-platform solution? Python dateutils print recurrence rule according to iCalendar format (see RFC 5545) How can I find $\lim_{n\to \infty} a_n$ Let $$a_n=\left(1-\dfrac{1}{\sqrt2}\right)\dots \left(1-\dfrac{1}{\sqrt{n+1}}\right),n\ge1$$ Then find $\lim_{n\to \infty} a_n$.   How can I proceed? I am stuck at the first step. Please help. General solution to the PDE $xU_x+yU_y=0$ Solve the equation $xU_x+yU_y=0$.   From Partial Differential Equations: An Introduction, 2nd Ed. (Strauss, pg. 10). There are no boundary conditions.  Solution: The PDE can be written $(x,y)\cdot\nabla U(x,y)=0$, implying that the vector $(x,y)$ is tangent to the PDE's characteristic equation. Then I can write $dy/dx=y/x$, and to solve this ODE,  $$\frac{dy}{dx}=\frac yx \\ \ln y=\ln x+c_1 \\ y=cx \\ y/x=c$$  Hence, the PDE is dependent on $c$ and I can write $U(x,y)=f(y/x)$ for some function $f$, solving the problem.  Question: Is this solution correct? The hint for the question (supplied by the professor) said I should use a substitution of variables, but this seems reasonably valid. How do you make sure email you send programmatically is not automatically marked as spam? using a:hover to change the color of an image New York is a great place to live. New York is a great place to live in.  I've seen the former usage a lot and I've started wondering what the grammar aspects of it are. The main question I'm asking involves the grammar when the preposition in the end of the sentence is deleted. Is it equally formal or correct to omit it? What are the grammar properties of this? Some more examples of similar usage:  This organisation is a great place to work. This is a great place to stay/sit/study/travel/go.  Just to show that 'live' isn't special in this case. Note: I'd posted the same question  before, but the answers were not satisfying enough (the answers there did not seem to provide the precise grammar aspects of this, those that could be in a serious grammar book), so I'm posting this here to get some more views on the question. I hope it is acceptable. Deriving Heaviside-Feynman formula for the electric field of an arbitrarily moving charge from Lienard-Wiechert potential How can I make a directory on the desktop?  I tried cd /desktop command but it gives an error  no such file or directory.  What's the correct command? 3d transforms on SVG element I am fairly familiar with Unity and I know how to work most of the features within it. But I don't know what programing languages to use for my Unity game. I have heard that it might be C#, but I am unfamiliar with C-sharp so I was wondering if Unity can use any other languages. I would not like opinions on which one to use, I just want to know what Unity supports. Why cant we replace diode connected BJTs by simple diodes in a current mirror circuit? In addition to the .tex and .dvi or .pdf files, TeX/LaTeX/ConTeXt produces and uses lots of other files. What do they do? Which ones are used to create the document and which ones are automatically created by the compiler. What files can I safely delete? Why do adult insects have 6 legs? Apparently, there is an advantage to having 6 legs in the insect world. What is that advantage, if anything? Why would such an advantage exist for insects, but not for other, larger land animals?  What do those middle legs do that the front and hind legs cannot, if anything? Are they really essential, or do they just generate more muscle power?