Home » Core Java » reflection

reflection

java.lang.reflect.Array Example

In this example we shall explain how to use java.lang.reflect.Array. The java.lang.reflect.Array class provides static methods to dynamically create and access Java arrays. Arrays are objects that contain values of the same type, with immutable length. So, an array’s components of the same type and their number is always fixed. The length of an array must be set when an ...

Read More »

Java Reflection Example

Java Reflection provides the ability to inspect and modify the runtime behavior of applications. Using reflection we can inspect a class or an interface, get their constructors, methods and fields information at runtime even though class is not accessible at compile time. We can also use reflection to instantiate an object, invoke it’s methods, change field values. The classes of ...

Read More »

Get package name

This is an example of how to get the package name of a class. Getting the package name of a class implies that you should: Create a new object of the class. Use getClass() API method of Object for the class to get the runtime class of this object. The returned Class object is the object that is locked by static ...

Read More »

Get super class of an object

With this example we shall show you how to get the super class of an object. To get the super class of an object implies that you should: Create a new Object. In the example we first create a new String object and then we create a new List object. Get the super class of the object, using getClass() API ...

Read More »

Get class from an object

In this example we shall show you how to get the class of an Object. To get the class that represents an Object one should perform the following steps: Create a new Object. Get the class of the Object, using getClass() API method of Object. It returns the runtime class of this Object. The returned Class object is the object that ...

Read More »

Get methods return type

This is an example of how to get the return type of a classe’s methods. To get the return type of a classe’s methods one should perform the following steps: Get the array of Method objects reflecting all the methods declared by the class, using getDeclaredMethods() API method of Class. This includes public, protected, default (package) access, and private methods, but ...

Read More »

Get Modifiers from an Object

In this example we shall show you how to get the modifiers of a class. To get the modifiers of a class one should perform the following steps: Call getModifiers() API method of Class to get the Java language modifiers for this class or interface, encoded in an integer. The modifiers consist of the Java Virtual Machine’s constants for public, protected, private, ...

Read More »

Invoke Method with reflection

With this example we are going to demonstrate how to invoke a method using reflection. In short, to invoke a method using reflection you should: Create a new StringBuilder with no characters in it and an initial capacity of 16 characters. Append a specified String to the builder using append(String str) API method of StringBuilder. Use getClass() API method to ...

Read More »

Get Methods from an Object

This is an example of how to get methods of an Object. Getting the methods of an Object implies that you should: Get the array containing Method objects reflecting all the public member methods of the class or interface represented by this Class object, using getMethods() API method of Class. Then get the array of Method objects reflecting all the ...

Read More »

Get Fields from an Object

In this example we shall show you how to get the fields of a class represented by an object. To get the fields of a class represented by an object one should perform the following steps: Get an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object, with getFields() ...

Read More »