HackerRank Java- Reflection - Attributes

JAVA reflection is a very powerful tool to inspect the attributes of a class in runtime. For example, we can retrieve the list of public fields of a class using getDeclaredMethods().

In this problem, you will be given a class Solution in the editor. You have to fill in the incompleted lines so that it prints all the methods of another class called Student in alphabetical order. We will append your code with the Student class before running it. The Student class looks like this:

  1. public class Solution {
  2. public static void main(String[] args) {
  3. Class student = Student.class; // uses class literal, not a function.
  4. Method[] methods = student.getDeclaredMethods();
  5. /* Get names from Methods */
  6. ArrayList<String> methodNames = new ArrayList();
  7. for (Method method : methods) {
  8. methodNames.add(method.getName());
  9. }
  10. /* Sort and print names */
  11. Collections.sort(methodNames);
  12. for (String name: methodNames) {
  13. System.out.println(name);
  14. }
  15. }
  16. }


Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube