Generics is a very powerful but often misunderstood enhancement to the Java language. It is available since Java5 and its addition remove the need to explicitly cast object, and also make Java programming fun, fast and safe by allowing modern IDEs like NetBeans, Intellij, Eclipse to display ‘Generics’ errors in instantly without compilation. Apparently, IDEs love statically-type language as they can take advantage of type information to perform magic like Auto-Complete, Refactoring with confidence etc. Prior to Generics, casting errors can only be exposed during runtime rather than compile time.
http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html
Generics. Don’t program without it.
One of the common questions ask for Generics in the internet but yet hard to find is how to instantiate a Generics Type.
So here the answer:
public class TestGenerics
{
public static void main(String[] _args)
{
// Number d = getInstance(Boolean.class); // Fail !
String d = getInstance(String.class); // OKay !
}
// Instantiate a Generic Type
public static <D> D getInstance(Class<D> _class)
{
try
{
return _class.newInstance();
}
catch (Exception _ex)
{
_ex.printStackTrace();
}
return null;
}
}
To decipher programming with Generics with ease, just imagine that the Generics Type is replaced by a concrete class, like String. This method ensures that the parameter class enter is the same as the return type. The IDEs will ensure that by flagging the error instantly the moment you enter the invalid code.




That doesn’t do anything for us.
Nope, this example doesn’t do anything useless but it is used to demonstrate the power of Generics and you can easier extend it after understanding it.
Give you a real useful example
By using Generics
1) No overloading method required
2) No casting required
This is for convenient purpose.
/* Usage: Note no casting is required.
JCheckBox _ch = createButton(JCheckBox.class, “Hello”);
JButton _ch = createButton(JButton.class, “Hello”);
JToggleButton _ch = createButton(JToggleButton.class, “Hello”);
*/
public D createButton(Class _button, String _action)
{
try
{
D _but = (D) _button.newInstance();
_but.setActionCommand(_action);
// Add rest of customization for AbstractButton subclass.
return _but;
}
catch (Exception e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return null;
}
Now you get the idea ?
I have five classes type1, type2,type3,type4,type5; and file holder classes:
(N=1,2,3,4,5)
public class HolderN {
List items = new ArrayList();
public void add(int id) {
for (int i = 0; i < id; i++) { items.add(new typeN()); }
}
public List getItems() { return items; }
public void setItems(List items) { this.items = items; }
}
Is there a way I can define a single Holder class for type1,type2,type3,type4,type5?
thanks
CAn try this.
public class MyNumber
{
List items = new ArrayList();
/* Note that Generic is not referencing object itself rather it is used to specify the type of existing object , therefore cannot
public void add(int id, T _c), I don’t think there is way to retrieve the type of T using reflection to avoid enter Class as argument because of limitation of Type erasure.
*/
public void add(int id, Class _c) {
try
{
for (int i = 0; i < id; i++) { items.add((T)_c.newInstance()); }
}
catch(Exception _e) {}
}
public List getItems() { return items; }
public void setItems(List items) { this.items = items; }
}
}
Do you mean public class MyNumber?
Otherwise (T) in items.add((T)_c.newInstance()) causes compile error.
thanks
Try to correct Bernard’s code.
I suspect that opening and closing bracket surrounding T characters are stripped by WordPress automatically and hence the error.
Replace [T] with greater and smaller brackets.
import java.util.List;
import java.util.ArrayList;
public class MyNumber[T]
{
List[T] items = new ArrayList[T]();
public void add(int id, Class[T] _obj)
{
try
{
for (int i = 0; i < id; i++)
{
items.add((T) _obj.newInstance());
}
}
catch (Exception _ex)
{
_ex.printStackTrace();
}
}
public List[T] getItems()
{
return items;
}
public void setItems(List[T] items)
{
this.items = items;
}
}
That will work.
thakn you, this helped