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!
===== Techniques ===== There are basic techniques involved in reflection: * Discovery β this involves taking an object or class and discovering the members, superclasses, implemented interfaces, and then possibly using the discovered elements. * Use by name β involves starting with the symbolic name of an element and using the named element. ====== Discovery ====== Discovery typically starts with an object and calling the {{Javadoc:SE|java/lang|Object|getClass()}} method to get the object's <code>Class</code>. The <code>Class</code> object has several methods for discovering the contents of the class, for example: * {{Javadoc:SE|name=getMethods()|java/lang|Class|getMethods()}} β returns an array of {{Javadoc:SE|java/lang/reflect|Method}} objects representing all the public methods of the class or interface * {{Javadoc:SE|name=getConstructors()|java/lang|Class|getConstructors()}} β returns an array of {{Javadoc:SE|java/lang/reflect|Constructor}} objects representing all the public constructors of the class * {{Javadoc:SE|name=getFields()|java/lang|Class|getFields()}} β returns an array of {{Javadoc:SE|java/lang/reflect|Field}} objects representing all the public fields of the class or interface * {{Javadoc:SE|name=getClasses()|java/lang|Class|getClasses()}} β returns an array of <code>Class</code> objects representing all the public classes and interfaces that are members (e.g. [[inner class]]es) of the class or interface * {{Javadoc:SE|name=getSuperclass()|java/lang|Class|getSuperclass()}} β returns the <code>Class</code> object representing the superclass of the class or interface (<code>null</code> is returned for interfaces) * {{Javadoc:SE|name=getInterfaces()|java/lang|Class|getInterfaces()}} β returns an array of <code>Class</code> objects representing all the interfaces that are implemented by the class or interface ====== 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)