Loading topic
java.lang.Class object. This chapter covers what that object is, three ways to get one, how the three differ, and what you can read from it right away. It assumes you know core Java syntax, basic generics, checked exceptions, and how to compile and run a pr5 min readClass object with .class, getClass(), or Class.forName(), and that you know core Java: fields, methods, constructors, access modifiers, and checked exceptions. Here you will use a Class object to list what a type declares and to read each member's4 min readnew for its class. This chapter covers how to find a constructor, call it, and handle the errors that come with it. It assumes you know what a Class object is, how to list members with getDeclaredConstructors(), and how checked exceptions wor4 min readClass object and list its members with getDeclaredFields(). If not, review the two chapters before this one. Here you will read and change field values on live objects, handle the exceptions that can occur, and avoid the usual traps with static, final, 4 min readClass object and list its members, as covered in the earlier chapters. You should also be comfortable with checked exceptions and try/catch. Here you will call methods by name, learn how arguments and return values behave, and handle the exception wra5 min readClass object and call getDeclaredField, getDeclaredMethod, Field.get and Method.invoke. It also assumes you know what a package is and how to compile and run a Java program from the command line. Here you will see why reflection fails on private m5 min read@interface and apply it to a class, and that you know the basics of Class and getAnnotation from earlier chapters. Here you will learn the four annotations that control how other annotations behave, and the mistakes that come from l4 min readnull result.4 min readClass object, and how to read a Field or Method through reflection. It covers what the compiler removes from generic types, what it keeps, and how to read the part that remains.4 min readMethod class and Method.invoke, how checked exceptions work, and how to write a Java interface and a lambda. It builds on the previous chapters about invoking methods and unwrapping InvocationTargetException.4 min readMethod, call it with Method.invoke, and handle checked exceptions. It adds java.lang.invoke.MethodHandle, which is a second way to call code you find at runtime. You will see how to create one, how it differs from Method.invoke, and when the dif5 min readClass, Field, Method, and setAccessible from the earlier chapters. Reflection works, but it costs speed, bypasses compile-time checks, and can break when the code around it changes. You will learn where those costs come from, how to reduce them, and when to6 min read@Retention policy, and read annotations with reflection. You should also be able to compile and run Java from the command line with javac and java. The chapter shows a different place to use annotations: the compiler it5 min read