Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Java Platform, Standard Edition
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====== Use by name ====== The <code>Class</code> object can be obtained either through discovery, by using the ''class literal'' (e.g. <code>MyClass.class</code>) or by using the name of the class (e.g. {{Javadoc:SE|name=Class.forName("mypackage.MyClass")|java/lang|Class|forName(java.lang.String)}}). With a <code>Class</code> object, member <code>Method</code>, <code>Constructor</code>, or <code>Field</code> objects can be obtained using the symbolic name of the member. For example: * {{Javadoc:SE|name=getMethod("methodName", Class...)|java/lang|Class|getMethod(java.lang.String,java.lang.Class...)}} β returns the <code>Method</code> object representing the public method with the name "methodName" of the class or interface that accepts the parameters specified by the <code>Class...</code> parameters. * {{Javadoc:SE|name=getConstructor(Class...)|java/lang|Class|getConstructor(java.lang.Class...)}} β returns the <code>Constructor</code> object representing the public constructor of the class that accepts the parameters specified by the <code>Class...</code> parameters. * {{Javadoc:SE|name=getField("fieldName")|java/lang|Class|getField(java.lang.String)}} β returns the <code>Field</code> object representing the public field with the name "fieldName" of the class or interface. <code>Method</code>, <code>Constructor</code>, and <code>Field</code> objects can be used to dynamically access the represented member of the class. For example: * {{Javadoc:SE|name=Field.get(Object)|java/lang/reflect|Field|get(java.lang.Object)}} β returns an <code>Object</code> containing the value of the field from the instance of the object passed to <code>get()</code>. (If the <code>Field</code> object represents a static field then the <code>Object</code> parameter is ignored and may be <code>null</code>.) * {{Javadoc:SE|name=Method.invoke(Object, Object...)|java/lang/reflect|Method|invoke(java.lang.Object,java.lang.Object...)}} β returns an <code>Object</code> containing the result of invoking the method for the instance of the first <code>Object</code> parameter passed to <code>invoke()</code>. The remaining <code>Object...</code> parameters are passed to the method. (If the <code>Method</code> object represents a [[static method]] then the first <code>Object</code> parameter is ignored and may be <code>null</code>.) * {{Javadoc:SE|name=Constructor.newInstance(Object...)|java/lang/reflect|Constructor|newInstance(java.lang.Object...)}} β returns the new <code>Object</code> instance from invoking the constructor. The <code>Object...</code> parameters are passed to the constructor. (Note that the parameterless constructor for a class can also be invoked by calling {{Javadoc:SE|name=newInstance()|java/lang|Class|newInstance()}}.)
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)