views:

73

answers:

3

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?

A: 

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

Tyler
+3  A: 

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.

erickson
Thanks. They shouldn't be static, though. I only showed a very small part of the code. :)
sidewaysmilk
Ah, okay. I just wanted to point out the difference between "static nested" and "inner" classes for consideration.
erickson
Thanks. I appreciate it, and revised the question to say "nested" rather than inner.
sidewaysmilk
+2  A: 

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.

Stephen C
It all definitely works very well. I'm aware of how scope and context work, but was more interested in whether using nested classes is commonly avoided. I'll amend my question.
sidewaysmilk