---
# System prepended metadata

title: Thinking in Java (Chapter 14)
tags: [OOP, Java]

---

Thinking in Java (Chapter 14)
===
###### tags: `Java` `OOP`
1. The class loader subsystem can actually comprise a chain of class loaders, but there’s only one primordial class loader, which is part of the JVM implementation. The primordial class loader loads so-called trusted classes, including Java API classes, typically from the local disk
1. the constructor is also a static method of a class
1. creating a reference to a Class object using ".class" doesn’t automatically initialize the Class object
1. If a static final value is a "compile-time constant," such as Initable.staticFinal, that value can be read without causing the Initable class to be initialized. Making a field static and final, however, does not guarantee this behavior: accessing Initable.staticFinal2 forces class initialization because it cannot be a compile-time constant.
1. If a static field is not final, accessing it always requires linking (to allocate storage for the field) and initialization (to initialize that storage) before it can be read
1. ordinary class reference can be reassigned to any other Class object, whereas the generic class reference can only be assigned to its declared type
1. To loosen the constraints when using generic Class references, I employ the wildcard, which is part of Java generics
1. Class bounded = int.class;
1. The reason for adding the generic syntax to Class references is only to provide compile-time type checking
1. when you use the generic syntax for Class objects: newlnstance( ) will return the exact type of the object, rather than just a basic Object
1. The cast( ) method takes the argument object and casts it to the type of the Class reference
1. The abstract getTypes( ) method defers to a derived class to get the List of Class objects (this is a variation of the Template Method design pattern)
1. IllegalAccessException relates to a violation of the Java security mechanism, in this case if the default constructor is private
1. Creates the List of Class objects using Class.forName( ). This may generate a ClassNotFoundException
1. the @SuppressWarnings annotation cannot be placed directly onto the static initialization clause.
1. the creation of types does not need to be surrounded by a try block since it’s evaluated at compile time and thus won’t throw any exceptions, unlike Class.forName( )
1. Instead of pre-loading the map, we can use Class.isAssignableFrom( ) and create a general-purpose tool
1. The generic parameter T allows create( ) to return a different type for each implementation of Factory. This also makes use of covariant return types
1. instanceof and islnstance( ) produce exactly the same results, as do equals( ) and ==. 
1. instanceof says, "Are you this class, or a class derived from this class?" On the other hand, if you compare the actual Class objects using ==, there is no concern with inheritance—it’s either the exact type or it isn’t.
1. the compiler must know about all the classes you’re working with.
1. Rapid Application Development (RAD) in an Application Builder Integrated Development Environment, which I shall refer to simply as an IDE.
1. discovering class information at run time is to provide the ability to create and execute objects on remote platforms, across a network. This is called Remote Method Invocation (RMI), and it allows a Java program to have objects distributed across many machines.
1. distributed computing also supports specialized hardware that might be good at a particular task—matrix inversions, for example—but inappropriate or too expensive for generalpurpose programming.
1. The class Class supports the concept of reflection, along with the java.lang.reflect library which contains the classes Field, Method, and Constructor (each of which implements the Member interface).
1. Constructors to create new objects, the get( ) and set( ) methods to read and modify the fields associated with Field objects, and the invoke( ) method to call a method associated with a Method object. In addition, you can call the convenience methods getFields( ), getMethods( ), getConstructors( ), etc., to return arrays of the objects representing the fields, methods, and constructors
1. with RTTI, the compiler opens and examines the .class file at compile time. Put another way, you can call all the methods of an object in the "normal" way. With reflection, the .class file is unavailable at compile time; it is opened and examined by the runtime environment.
1. Reflection is in the language to support other Java features, such as object serialization and JavaBeans
1. a class method extractor. Looking at a class definition source code or JDK documentation shows only the methods that are defined or overridden within that class definition.
1. Proxy is one of the basic design patterns. It is an object that you insert in place of the "real" object in order to provide additional or different operations
1. Java’s dynamic proxy takes the idea of a proxy one step further, by both creating the proxy object dynamically and handling calls to the proxied methods dynamically. All calls made on a dynamic proxy are redirected to a single invocation handler
1. You create a dynamic proxy by calling the static method Proxy.newProxyInstance( ), which requires a class loader that you wish the proxy to implement, and an implementation of the interface InvocationHandler. The dynamic proxy will redirect all calls to the invocation handler
1. The invoke( ) method is handed the proxy object, in case you need to distinguish where the request came from
1. A variant of Null Object is the Null Iterator pattern, which makes iteration over the nodes in a composite hierarchy transparent to the client (the client can then use the same logic for iterating over the composite and leaf nodes).
1. In general, the Null Object will be a Singleton, so here it is created as a static final instance. This works because Person is immutable—you can only set the values in the constructor, and then read those values, but you can’t modify them
1. Extreme Programming (XP), as is "Do the simplest thing that could possibly work."
1. Command pattern
1. Logical variations of the Null Object are the MocA: Object and the Stub. Like Null Object, both of these are stand-ins for the "real" object that will be used in the finished program. However, both Mock Object and Stub pretend to be live objects that deliver real information, rather than being a more intelligent placeholder for null, as Null Object is.
1. Mock Objects tend to be lightweight and self-testing, and usually many of them are created to handle various testing situations. Stubs just return stubbed data, are typically heavyweight and are often reused between tests. Stubs can be configured to change depending on how they are called
1. An important goal of the interface keyword is to allow the programmer to isolate components, and thus reduce coupling.
1. interfaces are not airtight guarantees of decoupling
1. Using RTTI, we discover that a has been implemented as a B. By casting to B, we can call a method that’s not in A
1. it’s still possible to reach in and call all of the methods using reflection, even private methods! If you know the name of the method, you can call setAccessible(true) on the Method object to make it callable
1. javap -private C

