Entrepreneurship and Innovation: What, How and Why

Create a class of the type provided in the first parameter.

Create an object from the class created in the previous step.

Get the value of the property specified in the second parameter.

Modify the value of the property obtained in the previous point with the value of the third parameter.

Below I show the same example in the belize phone number library Groovy language. The concept is the same, it’s just that Groovy’s features allow the syntax to be simpler.

To run the example you only need to compile how to start a business after retirement the Client class using the command:

 

Groovyc EjemploReflexion groovy

 

And then run the ExampleReflection Script using the command:

groovy ExampleReflection australia aatabase airectory Client name Josue

In case you want to run the example with another class, the syntax would be:

groovy ReflectionExample.groovy [ClassName] [PropertyName] [Value]

The result will be equivalent to the example with java

 

Conclusion

Reflection is a very useful capability provided by most modern programming languages ​​such as Java, Groovy, Python, Ruby, C#, among others. It allows us to know the internal structure of a class, its properties, constructors, methods (their parameters), execute the methods, and even obtain and modify information about private members of a class. All this without needing to have the source code or documentation of a class. Also through reflection we can make a program dynamically adapt to different situations, at runtime.

Leave a Comment

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

Scroll to Top