I'm looking at a tutorial found on: http://www.ibm.com/developerworks/library/j-dyn0603/
In particular there is a section where it gives the following example:
Class[] types = new Class[] { String.class, String.class };
Constructor cons = TwoString.class.getConstructor(types);
Object[] args = new Object[] { "a", "b" };
TwoString ts = (TwoString)cons.newInstance(args);
I don't quite follow what Class[] represents. The way I read it, this says 'an array of Class objects called types]. I'm also somewhat unfamiliar with the syntax used in the new statements - how does new Class[] { String.class, String.class} work?
If someone can help break this down for me I would appreciate it.
Yes the literal meaning is exactly what you are thinking it is
Class[] types = new Class[] { String.class, String.class };
is a declaration and initialization in one line. It says create an array that holds objects of type Class
and initialize it with two objects of type Class
, namely String.class and String.class.
A similar example would be
int[] nums = new int[]{1,2,3};
or
float[] decimals = new float[]{1.2, 3.1, 5.2}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With