Is Java dot a new code smell construct?

I recently got the chance to use Java <instance> .new to instantiate an inner class.

Although I believe there was a reasonable excuse for this, its rather obscure syntax seems to make things more difficult for service programmers to understand.

But I wonder if the need for <instance> .new is an indicator that it would be better to do a bit of refactoring to make the inner class a top-level class in its parent package, give it a constructor that accepts a reference to its parent type will be more ideal and will add access methods or access modifiers at the package level to the fields that it accesses from the parent type.

Are there any use cases for <instance> .new that cannot be handled by refactoring the inner class to the outer class?

+8
java instantiation class inner-classes code-smell
source share
5 answers

I would not say that this is necessarily the smell of code.

If the connection between the outer and inner classes is strong, and your use of the inner class is a good encapsulation, I would say that your use of this construct is justified. On the other hand, if the abstraction is already running, then creating an inner class of the top-level class is probably the best idea.

Using outer.new Inner() may indicate a fuzzy abstraction, or it may not be.

(The design was included in the language for a good reason, and the fact that it is rarely needed does not invalidate it.)

+2
source share

Are there any use cases for .new that cannot be handled by refactoring the inner class to the outer class?

There are usually no important use cases in this syntax since you can always hide the inner constructor and let the outer class return instances of the inner class using factory methods (if you intend to divulge the inner type in appearance anyway)

 public class Outer { public class Inner { private Inner() {} } public Inner createInner() { return new Inner(); } } 

Client Code:

 Outer outer = new Outer(); Inner inner = outer.createInner(); 

Thus, the "smell of code" (if it is really the smell of code) is not that the inner class is present, but that its constructor is public (and used). But after a thorough assessment, you can see that this is normal

UPDATE A good sign to indicate that encapsulation is broken is when Outer instances probably have a shorter lifespan than Inner instances. In this case, Inner instances may contain a reference to Outer for a longer period of time than intended by the author of Outer . This can even lead to memory leaks.

+4
source share

Well, from the inner class, you have access to private members of the outer class. Top-level classes do not.

But you can add a factory method to the outer class, which creates a new instance of the inner class.

0
source share

Using internal classes makes it difficult to understand the code, because classes share their internal state and therefore violate the principle of information hiding. Sometimes using an inner class can make the solution easy to write, but I think this is not the best / clean solution most of the time.

0
source share

I use espescially inner class when class method needs to be multithreaded.

Indeed, there are two clean ways to declare threads:

  • Create a Runnable class (containing a method that receives multithreading), and let the client initiate a new thread. The disadvantage is that the client must know that he has to deal with a multi-threaded process.

  • Do not declare a Runnable job class, but prefer to define an inner-class that implements Runnable . Of course, this inner-class does not have to be public and therefore is backed up by one of the public methods in the outer class (which means the job class). Thus, the client deals with this public method, not knowing that the stream was created in the background. If tomorrow developers want to remove multithreading within this class of tasks for any reason, the client can be completely saved without changes.

And to allow this inner class to inherit the fields of the outer class, I do not declare it static. (also called a nested class, as opposed to an inner class)

0
source share

All Articles