Java: Accessing Constructors, Methods, & Fields Using Reflection
Java
| Intermediate
- 13 videos | 1h 38m 12s
- Includes Assessment
- Earns a Badge
Reflective access allows a whole range of operations on objects that may not be permitted when you construct and use objects in the regular manner. This is extremely useful while building frameworks that may need access to the internals of your object. Use this course to extract information and metadata about the constructors in a class and use them, accessed via reflection, to instantiate objects. Access and modify fields or the member variables in an object. Access and invoke the methods defined on a class. Invoke static methods on the class itself and instance methods on specific objects of a class. When you're done, you'll have the ability to use reflection to access and work with all class members, whether they are fields, constructors, or methods.
WHAT YOU WILL LEARN
-
Discover the key concepts covered in this courseAccess the constructors in a class using reflectionCreate objects of a class with handles to constructorsAccess member variables of a class and its metadataAccess and update values of fieldsAccess and update protected and private fieldsAccess public, private, and protected methods
-
View method parameters, annotations, return types, and exceptionsView annotations on methods and recall the use of the retention policyObserve how not all annotations are available at runtime for reflective accessUse method heuristics to identify getters and setters in a classInvoke and call methods using handlesSummarize the key concepts covered in this course
IN THIS COURSE
-
2m 57sIn this video, you’ll learn more about your instructor and this course. In this course, you’ll learn how to extract information and metadata about the constructors in a class. You’ll see how the .getConstructors method only returns public constructors. You’ll learn that to access the private or protected constructors, you’ll need the .getDeclaredConstructors method. Then, you’ll explore metadata about fields using Reflection. FREE ACCESS
-
9m 33sHere, you’ll watch a demo. In this demo, you’ll learn to instantiate objects of a given class. First, you’ll examine the Java docs for the class you see mentioned onscreen. These refer to using the reflection API's in order to construct new instances of a class. You’ll see a blank Main.java and an Employee.java file as well. FREE ACCESS
-
8m 54sHere, you’ll watch a demo. You’ll pick up where you left off at the end of the last demo. There, you used the newInstance method on a constructor object. You used that to create an object of a specific class. That code is onscreen now. It's the employeeClass you’ll be instantiating in this manner. This time it's the noArgumentConstructor you’ll use. FREE ACCESS
-
10m 14sHere, you’ll watch a demo. You’ll learn more about fields. In the context of a reflection, the term field is used for member variables. Member variables whether private, public, or protected, static or non-static, are all covered by the reflection API for fields. This does not include methods. Those are covered by a different API. FREE ACCESS
-
8m 52sHere, you’ll watch a demo. Here, you’ll clear up the current contents of your main method and then add in a new import statement that you’ll need. This is java.lang.reflect.Modifier. It allows you to interpret the modifiers applied to a field or a member function. Onscreen you’ll see the code you’ll need for fields in your employeeClass. You’ll use the class.forName method. It takes in a string and returns the class object. FREE ACCESS
-
9m 13sHere, you’ll watch a demo. Here, you’ll run the code and examine the output you see onscreen. You can see from the output that initially, your employee object has no real values for any of its fields. Initially, the only field which is populated is the ID. But the Name and Title are both unknown, the salary is 0, and the Department and list of Committees are both set to null. FREE ACCESS
-
8m 36sHere, you’ll watch a demo. You’ll work with methods, which are member functions. You’ll begin by importing a specific class. This is a java.lang.reflect.Method. This class contains much of the functionality in the Java Reflection APIs that's relevant to working with methods. You’ll note a method can include an abstract method as well. Onscreen, you’ll see the URL which hosts the Java Docs for java.lang.reflect.Method. FREE ACCESS
-
7m 14sHere, you’ll watch a demo. You’ll turn your attention to a user-defined class, specifically the employee class, which you now see onscreen. This particular iteration of the employee class is slightly different than in previous demos. That's because your focus is on many different types of methods rather than fields or constructors. You’ll see a few fields on lines 10 through 15, and then on line 17, you have a constructor that's private. FREE ACCESS
-
7m 8sHere, you’ll watch a demo. In this demo, you’ll learn how reflection and annotations interact. You’ll begin by importing additional bits of functionality. Onscreen you’ll see you’ve imported java.lang.reflect.Modifier,and. You’ll then add the required code. You’ll factor out a code into a separate method called printMethodProperties. This is defined onscreen starting on line 9. printMethodProperties takes in one input argument which is of type method. FREE ACCESS
-
8m 12sHere, you’ll watch a demo. In this demo, you’ll recognize annotation runtime policy implications. You’ll learn the printMethodProperties utility can be very helpful. It provides you with information about any method that you pass in. You’ll invoke it one more time now. You first need to get a method object. You’ll do this on line 47 by invoking employeeClass.getMethod. You’ll pass in the name of the method you’re looking for. FREE ACCESS
-
10m 14sHere, you’ll watch a demo. In this demo, you’ll move on to another aspect of using reflection to invoke methods on a specific object. This is a logical next step and it's an important operation in real-world uses of reflection. Onscreen, you’ll see a method called isGetter defined. This is a private static method. It returns a boolean value. It takes in a method object. FREE ACCESS
-
4m 32sHere, you’ll watch a demo. In this demo, you’ll move on to changing the state of an object by invoking the setter methods on that object. First, you’ll add in some additional code. Onscreen, you’ll see you have yet another for loop. This one starts on line 83. This for loop also iterates over all of the declared methods in our employeeClass. This for loop has a loop variable called method with the lowercase m. FREE ACCESS
-
2m 33sIn this video, you’ll summarize what you’ve learned in this course. You’ve learned how to extract information and metadata about the constructors in a class. You saw how the .get constructors method only returns the public constructors in a class. You used Reflection to obtain handles to individual constructors in a class. You created new objects of the class using those handles via the .new instance method. FREE ACCESS
EARN A DIGITAL BADGE WHEN YOU COMPLETE THIS COURSE
Skillsoft is providing you the opportunity to earn a digital badge upon successful completion on some of our courses, which can be shared on any social network or business platform.
Digital badges are yours to keep, forever.