Вы находитесь на странице: 1из 1

Provide a simple example of object reflection in Java. What is reflection in Java?

The word reflection implies that the properties of whatever being examined are displayed or reflected to someone who wants to observe those properties. Similarly, Reflection in Java is the ability to examine and/or modify the properties or behavior of an object at run-time. Its important to note that reflection specifically applies to objects so you need an object of a class to get information for that particular class. Reflection is considered to be an advanced technique, and can be quite powerful when used correctly.

Reflection in Java consists of 2 primary things that you should remember: 1. Metadata. Metadata literally means data about the data. In this case, metadata means extra data that has to do with your Java program like data about your Java classes, constructors, methods, fields, etc. 2. Functionality that allows you to manipulate the metadata as well. So, functionality that would allow you to manipulate those fields, methods, constructors, etc. You can actually call methods and constructors using Java reflection which is an important fact to remember.

Java reflection is part of an API package


In order to use reflection in Java, you must include the necessary classes from the java.lang.reflect package since that contains all the necessary classes for implementing reflection. The one exception to this is when retrieving class metadata for which you would use the java.lang.Class class, which is not part of the java.lang.reflect package. Lets look at an example of Java reflection to help clarify exactly what it does and when it can be useful.

Вам также может понравиться