Java: Accessing Constructors, Methods, & Fields Using Reflection

Java    |    Intermediate
  • 13 videos | 1h 38m 12s
  • Includes Assessment
  • Earns a Badge
Rating 4.3 of 15 users Rating 4.3 of 15 users (15)
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 course
    Access the constructors in a class using reflection
    Create objects of a class with handles to constructors
    Access member variables of a class and its metadata
    Access and update values of fields
    Access and update protected and private fields
    Access public, private, and protected methods
  • View method parameters, annotations, return types, and exceptions
    View annotations on methods and recall the use of the retention policy
    Observe how not all annotations are available at runtime for reflective access
    Use method heuristics to identify getters and setters in a class
    Invoke and call methods using handles
    Summarize the key concepts covered in this course

IN THIS COURSE

  • 2m 57s
    In 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 33s
    Here, 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
  • Locked
    3.  Creating Objects Using Constructor Handles
    8m 54s
    Here, 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
  • Locked
    4.  Accessing Field Metadata
    10m 14s
    Here, 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
  • Locked
    5.  Getting and Setting Field Values
    8m 52s
    Here, 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
  • Locked
    6.  Accessing and Updating Private and Protected Fields
    9m 13s
    Here, 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
  • Locked
    7.  Accessing Public, Private, and Protected Methods
    8m 36s
    Here, 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
  • Locked
    8.  Viewing Method Parameters, Annotations, and More
    7m 14s
    Here, 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
  • Locked
    9.  Accessing Annotations on Methods
    7m 8s
    Here, 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
  • Locked
    10.  Recognizing Annotation Runtime Policy Implications
    8m 12s
    Here, 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
  • Locked
    11.  Identifying Getters and Setters Using Heuristics
    10m 14s
    Here, 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
  • Locked
    12.  Invoking Functions Using Reflection
    4m 32s
    Here, 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
  • Locked
    13.  Course Summary
    2m 33s
    In 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.

YOU MIGHT ALSO LIKE

Rating 3.7 of 11 users Rating 3.7 of 11 users (11)
Rating 4.6 of 57 users Rating 4.6 of 57 users (57)

PEOPLE WHO VIEWED THIS ALSO VIEWED THESE

Rating 3.7 of 11 users Rating 3.7 of 11 users (11)
Rating 4.4 of 38 users Rating 4.4 of 38 users (38)
Rating 4.6 of 4717 users Rating 4.6 of 4717 users (4717)