What are the significant differences between anonymous and predefined Java classes?

I have a large tree data structure of objects that behave basically the same, but differ in one or two methods that calculate some of the keys used to navigate the structure. The divergent behavior depends on where the objects are in the structure.

I started out with an abstract base class and had several subclasses that implement each type of behavior. This gives me about ten subtypes, which a) are hard to name sensibly and b) look a little cumbersome in the project source folder as they are so similar.

I would rather have a single factory class that removes instances of anonymous subclasses on the fly. It would give me a lot of flexibility and open the door to many nice improvements like data sharing and parameterization of things, and it would look much cleaner in my code structure. However, this is all very memory sensitive and memory access timing, and I would have a lot of such objects. Should I be aware of any disadvantages or peculiarities of anonymous classes?

+2


source to share


4 answers


Similar to non-static inner classes, anonymous classes have a hidden reference to the class in which they are defined, which can cause problems if you use serialization and of course don't allow objects of the outer class to be GC-qualified - but this is unlikely to be a problem if you do it in one factory class.



+4


source


Anonymous classes are no different from named classes.

But yes, having multiple objects can affect your memory size and performance (garbage collection).




From what you say, I wonder if it is possible to split your class into two parts:

  • All constant methods in one class (no subclass of that class).
  • All variable methods (see below) are encapsulated in the position interface. You can have multiple classes that implement it. Objects of these classes will be stateless, so they can be shared instances which are great for performance and memory).

Variable methods: calculate some keys depending on the position in the structure.

+3


source


As mentioned, an anonymous inner class usually has a hidden reference to the class in which it is declared. However, you can fix this by declaring an anonymous class from a static method (simple, not completely obvious).

The main disadvantage of this method is that the class names visible in the jars will be numbered (eg "MyClass $ 0.class") and won't be easily identified in the stacktraces (except using line numbers of course) and without the toString () methods not easy to identify in your own println statements.

Declaring static inner classes is a great technique. It will eliminate all these flaws and preserve the file hierarchy. Also consider making these inner classes private or final if you don't need to extend them.

+2


source


A class is a class. It doesn't matter if they are "top-level classes", regular inner class, local inner class, or anonymous inner class.

Non-static inner classes or inner classes that access the private members of their enclosing class will have a tiny bit of extra code in them. For non-static inner classes, the compiler adds a member variable that refers to the enclosing instance. If the inner class refers to any private members of the enclosing class, the compiler will synthesize the accessory in the enclosing class with the "close package" accessibility (default).

+1


source







All Articles