digitization, transformation, binary

Class Method in Java

Class Method in Java

Method class represents a method. Instances of Method are obtained by calling the getMethod() and related methods of java.lang.Class.

The Method class represents a method of a class. A Method object can be obtained by calling the
getMethod() method of a Class object. The method provides methods for getting the name, modifiers, return type, parameters, exceptions, and declaring class of a method. The invoke() method can be used to run the method.

The method implements the Member interface, so you can use the methods of that interface to obtain the method name, modifiers, and declaring class. In addition, getReturnType(),
getParameterTypes(), and getExceptionTypes() also return important information about the represented method.

Perhaps most important, the invoke() method allows the method represented by the Method object to be invoked with a specified array of argument values.

If any of the arguments are of primitive types, they must be converted to their corresponding wrapper object types in order to be passed to invoke().

If the represented method is an instance method (i.e., if it is not static), the instance on which it should be invoked must also be passed to invoke(). The return value of the represented method is returned by invoke().

If the return value is a primitive value, it is first converted to the corresponding wrapper type. If the invoked method causes an exception, the Throwable object it throws is wrapped within the InvocationTargetException that is thrown by invoke().

READ  Static Keyword Simplified in Java

The structure of the class Method is given by

public final class java.lang.reflect.Method extends java.lang.Object implements java.lang.reflect.Member {
// No Constructor
// Public Instance Methods
public boolean equals(Object obj); // Overrides Object
public Class getDeclaringClass(); // From Member
public Class[] getExceptionTypes();
public native int getModifiers(); // From Member
public String getName(); // From Member
public Class[] getParameterTypes();
public Class getReturnType();
public int hashCode(); // Overrides Object
public native Object invoke(Object obj, Object[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException;
public String toString(); // Overrides Object
} 

The details of the class structure are given as follows:

public boolean equals(Object obj);

public boolean equals(Object obj) method returns true if obj is an instance of Method, and it is equivalent to this Method.

This method returns true if the objects are equal; false if they are not.

Parameter
obj – The object to be compared with this object.

public Class getDeclaringClass();

public Class getDeclaringClass() method returns the Class object for the class in which this method is declared.

This method returns the Class object that represents the class that declared this method.

public Class[] getExceptionTypes();

public Class[] getExceptionTypes() method returns an array of Class objects that represents the throws clause of this method. If the method does not throw any exceptions, an array of length 0 is returned.

This method returns an array that contains the Class objects that describe the exceptions that can be thrown by this method.

public native int getModifiers();

public native int getModifiers() method returns an integer value that represents the modifiers of this method. The Modifier class should be used to decode the returned value.

READ  Class ByteArrayInputStream in Java

This method returns an integer that represents the modifier keywords used to declare this method.

public String getName();

public String getName() method returns the name of this method.

This method returns the name of this method as a String.

public Class[] getParameterTypes();

public Class[] getParameterTypes() method returns an array of Class objects that represents the parameter types this method accepts. The parameter types are listed in the order in which they are declared. If the method does not take any parameters, an array of length 0 is returned.

This method returns an array that contains the Class objects that describe the parameter types that this method accepts.

public Class getReturnType();

public Class getReturnType() method returns the Class object for the type that this method returns.

This method returns the Class object that represents the return type of this method.

public int hashCode();

public int hashCode() method returns a hashcode for this Method.

This method returns a hashcode for this object.

public native Object invoke(Object obj, Object[] args);

public native Object invoke(Object obj, Object[] args) method executes the method represented by this object on the given object using the given array of arguments. If the method is declared static, the obj parameter is ignored. Otherwise, the object supplied must be an instance of the class that declares this method.

If a particular parameter is of a primitive type, the corresponding argument is automatically unwrapped and converted to the appropriate type, if possible. If that is not possible, an
IllegalArgumentException is thrown. If the method itself throws an exception, the exception is
placed in an InvocationTargetException, which is then thrown to the caller of invoke().

READ  Abstract Interface Checksum in Java

If the method completes normally, the value it returns is returned. If the value is of a primitive type, the value is wrapped in an appropriate object and the object is returned. If the return type is void, null is returned.

This method returns an Object that contains the return value of the invoked method.

Parameter
obj – The instance upon which this method is invoked.
args – An array of arguments to be passed to this method.

public String toString();

public String toString() method returns a string representation of this Method. This string contains the access modifiers of the method, if any, followed by the return type, the fully qualified name of the declaring class, a period, the name of the method, and a list of the parameters of the method if any.

The list is enclosed by parentheses and the individual parameters are separated by commas. If the method does not have any parameters, just the parentheses are included in the string.

This method returns a string representation of this object.
Apart from these Method class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *