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.
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}
A Class
is an Object
that describes a java class (or interface). A Class[]
is an array of these objects.
The syntax <ClassName>.class
(e.g. String.class
) returns the Class
for a particular java class (or interface).
A new array can be created through a few syntax. new Class[i]
creates a new array of Class
with length i
and filled with default values (null references). new Class[]{a, b, c}
creates a new array of Class
containing the given elements a
, b
and c
. It's length is 3 because 3 elements were given.
That's an array of the type Class
The
new Class[] {...}
Creates and array of type Class
whose contents are listed inside.
In general
Type[] means, an "array of..." just like you thought.
It turns out the type here is Class
which in Java is a representation of a running class or interface.
Probably you shouldn't go for advanced features like reflection without knowing first the basics.
It is an array of Objects which share the super type Class
.
In Java, everything is an Object
. This means that if you want to have in-code awareness of class types, you need to create an Object
which can encapsulate the details of the class. Java calls this Object
a Class
.
Note the capitalization and be aware that since Class
is an Object
, you can call all Object
methods on class, like x.getClass()
, x.wait()
, and x.hashcode()
.
Class[] items = { String.class, Integer.class };
means create an array called items which contains Class
objects. Initialize the array with the Object
s representing the class of String
and the class of Integer
.
Since Class
objects represent class types, often the two are used interchangeably in informal speech.
Class
is actually a class named, uhmm.. Class.Instances of the class Class represent classes and interfaces in a running Java application.
new Class[]
works. It initializes an array of Class references. But, as usual with object arrays, the references are null.Class
Class
does not have a public constructor. So you can not saynew Class()
Class
is a java class that represents ... a class and related metadata. so Class[]
is an array of Class
objects.
Each class in java has a static member variable named class of type Class and here what you're doing is
- creating a Class[] containing two members each of which is an object of type Class and specifically the Class representing java.lang.String
- looking up via reflection the reference to the constructor in the class TwoString that takes two java.lang.String objects => TwoString(String, String)
- creating an object array to feed to the constructor with the values "a" and "b"
- finally you're creating an instance of TwoString ts by invoking the constructor you looked up in (b) and args you initialized in (c)
This is equivalent to:
TwoString ts = new TwoString("a","b");
but the way you've posted is using Reflection/Introspection.
I am not sure if this is what you are looking for, but the Class[]
in the example is mimicking the constructor in that TwoString class. To me it would have been clearer if they would have used two different types. So if the TwoString class looked like this:
public class TwoString {
private String m_s1;
private Integer m_s2;
public TwoString(String s1, Integer s2) {
m_s1 = s1;
m_s2 = s2;
}
}
The class name would not make since anymore, but I think that is easy to fix. The rest of the code example would become:
Class[] types = new Class[] { String.class, Integer.class };
Constructor cons = TwoString.class.getConstructor(types);
Object[] args = new Object[] { "a", 1 };
TwoString ts = (TwoString)cons.newInstance(args);
So the class array is to set up the constructor to use. The steps line by line are:
- The class array sets up what constructor to use.
- Then you find the constructor.
- The you set up what values to pass into the constuctor.
- Then you call the constructor to create the instance.
精彩评论