Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

Making Objects

Once you've identified the classes and interfaces you're interested in, you'll want to figure out where instances come from.

The easiest case is when they've provided a constructor in the class. Even then your problem isn't necessarily solved, however, because many constructors take instances of other objects, and now you're going to have to figure out how to make one of them.

The obvious place to start is by scanning the code for expressions like new Foo, where Foo is the class you're interested in. Unfortunately, several things can get in the way. If Foo is an interface or abstract class, you can't create an instance of Foo directly. You can go looking around for expressions like extends Foo or implements Foo, but a simple text search isn't going to help you all the time here, because of things like this:

class Bar implements Qux, Foo

or this:

class Bar implements

Javadoc can come to the rescue again, although a bit differently. Try using the –private flag. It produces documentation of all the methods, not just the public and protected ones. That will tell you what all the implementing classes of an interface are, not just the public ones.

It's not uncommon for a programmer to create a public interface and a private class corresponding to that interface. That gives the programmer the flexibility to swap out implementation, or provide multiple implementations, without telling coworkers about the design change. The full Javadoc will tell you where they are.

  • + Share This
  • 🔖 Save To Your Account