Justin Force
Justin Force

Reputation: 6342

Is there anything wrong with using private classes in my Java Servlet?

I have Servlet that looks something like this:

public class MyServlet extends Servlet {

  private class Page {

    private Page(HttpServletRequest request, HttpServletResponse response) {
      /* Do stuff */
    }
  }

  protected void doGet(HttpServletRequest request, HttpServletResponse response) {

    Page page = new Page(request, response);

    /* Do other stuff */
  }

}

I also have a Cache that looks something like this:

public class Cache {

  private Hashtable<String, CacheValue>;

  public Cache() {
    Hashtable<String, CacheValue> table = new Hashtable()<String, CacheValue>;
  }

  public void put(String key, String value) {
    table.put(key, new CacheValue(value));
  }

  private class CacheValue {

    private value;

    private CacheValue(String value) {
      this.value = value;
    }
  }
}

My question is this: is there anything wrong with using private nested classes in this way? I know that you'd usually have a separate public class in a separate file, but a CacheValue is only ever used by a Cache. A Page is only ever used by a MyServlet. Organizing the classes this way makes sense to me, but I'm no old pro at Java, so I'm interested in the pros and cons.

Is using nested classes generally regarded as a matter of style? Or of preference? Or something to be avoided? Or a tool to reduce complexity?

Upvotes: 0

Views: 1508

Answers (3)

Stephen C
Stephen C

Reputation: 719249

My question is this: is there anything wrong with using private inner classes in this way? I know that you'd usually have a separate public class in a separate file, but a CacheValue is only ever used by a Cache.

Nothing wrong with this. The Page class is a private nested class. By definition nested classes live in the same file as their enclosing classes.

This is fine, in general and in the context of a servlet. If there were problems with (for example) visibility of the private class or its members, then the compiler would tell you.

EDIT

Is using nested classes generally regarded as a matter of style? Or of preference?

It is hard to say which. I guess, appropriate use of nested classes is good style, especially if you declare them static when they don't need to be non-static. However, I don't think that the canonical Java style guidelines mention it.

Or something to be avoided?

No.

Or a tool to reduce complexity?

Yes ... sort of. I tend to view nested classes (and especially private nested classes) as a modularization mechanism. It doesn't reduce complexity in the "cyclomatic complexity" sense, but it certainly helps to hide / close off implementation details.

Upvotes: 2

erickson
erickson

Reputation: 269797

No, there's nothing wrong with keeping things private.

When code is less accessible, fewer things depend on it, and that gives you more freedom to make modifications.

However, you should declare your CacheValue and Page classes static, because they don't access any members of an enclosing instance.

Upvotes: 3

Tyler Smith
Tyler Smith

Reputation: 1279

As long as the only calling of those functions happens inside that class you shouldnt have any problem with them being private.

Upvotes: 0

Related Questions