1

So I have a ClassA:

 public ClassA {
     String key;
     List<ClassB> value;
 }

And this ClassA is mapped to a database table (with 2 columns having key -> list of values) and the values here get stored as a row in there.

public ClassB {
    Integer data1;
    ...
    String dataN;

   /* constructors and some getters/setters follow */
}

To clarify, ClassB just contains some data that is being stored in database.

When ClassA is being saved, List<ClassB> is being converted to JSON string and getting saved.

There are 2 ways to define ClassB.

  1. Either have it as a regular class
  2. Define it as a inner class(not sure if static or not) inside classA.

ClassB is currently not being used anywhere else in the project.

What do you think should be the right way and why?

I am bit confused regarding nested classes and I cannot distinguish if they are being misused or not.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
AgentX
  • 1,402
  • 3
  • 23
  • 38
  • 3
    Either one is fine. It's up to you. If you can't decide, try one way, then revisit it later. – John Kugelman Feb 13 '15 at 18:45
  • i am afraid you want it to be static if you choose to make it inner. either way would work. – Jason Hu Feb 13 '15 at 18:48
  • 1
    While I like this question - it's well-formed and an understanding of practices is present, it seems more open to opinion than anything. – Drew Kennedy Feb 13 '15 at 18:50
  • Yeah, it is more of a personal choice here. Now even if I use a nested class I am confused about keeping it static or non-static? – AgentX Feb 13 '15 at 18:54
  • To avoid highly opinionated answers you should include what `B`'s purpose is; that way the answer can actually be quantifiable. – apxcode Feb 13 '15 at 18:54

2 Answers2

1

Personally, if the class is small (for example just an helper) and is not to be used anywhere else, I would prefer doing an inner class. However, this is mostly a matter of opinion.

I think the best in these case is to make sure everyone in your dev team work the same way so it is easier for everyone to debug.

Note that there is a difference between inner class and nested class. A nested (static) class is an inner class declared static, while a simple inner class is normally not static.

Nested static class can be accessed anywhere using Class.NestedStaticClass.

See Nested class documentation for more details and example.

Here an interesting quote from the link I gave u before :

Serialization of inner classes, including local and anonymous classes, is strongly discouraged. When the Java compiler compiles certain constructs, such as inner classes, it creates synthetic constructs; these are classes, methods, fields, and other constructs that do not have a corresponding construct in the source code. Synthetic constructs enable Java compilers to implement new Java language features without changes to the JVM. However, synthetic constructs can vary among different Java compiler implementations, which means that .class files can vary among different implementations as well. Consequently, you may have compatibility issues if you serialize an inner class and then deserialize it with a different JRE implementation. See the section Implicit and Synthetic Parameters in the section Obtaining Names of Method Parameters for more information about the synthetic constructs generated when an inner class is compiled.

You might also consider using Anonymous inner class. An anonymous inner class is a class coded directly in the instanciation. For example

new ParentClassName(constructorArgs) {
   members..
}
Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
1

ClassB is currently not being used anywhere else in the project.

I think the key word here is "currently".

If you imagine a situation in which ClassB might be useful in other places in the project (say, if that project grows in a particular way, or if there are other tables that might map to the same structure in the future), then it should probably be a "normal" class.

If the class is logically tied to ClassA. For example, ClassA represents a train and ClassB train cars, which are always related to trains and never to other vehicles which are not trains, then you should define it as a nested class or inner class of ClassA.

Whether to make it nested or inner depends on the type of connection between an object of class ClassB and one of ClassA. It's not always a clear-cut issue, but remember that static nested classes can exist independently of their parent class. (e.g. you can manufacture a train car before you ever create a train object that it will be part of, and you can move train cars between trains), while inner classes always contain an invisible reference to their parent object, and such an object has to exist before you can create an object of the inner class.

All else being equal, I think I would gamble on a static nested class as an initial solution. If I realize that there are other places that need the same class, it's going to be relatively easy to refactor it.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79