Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Java Platform, Standard Edition
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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()}}.)
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Java Platform, Standard Edition
(section)
Add topic