Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class[] - What does it mean?

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.

like image 947
nazbot Avatar asked Mar 04 '11 21:03

nazbot


1 Answers

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}
like image 178
Matt Phillips Avatar answered Sep 27 '22 22:09

Matt Phillips