Inner Classes Vs. Nested Classes in Java

Java has something called inner classes. Most of you know what an inner/nested implies in an object-oriented programming language. When we talk about nested we are referring to something (a element or block) of the same kind as the outer kind declared within the inner body of the outer element. Nested is simply a more specific term for an inner element. You can have an inner if statement within a loop for example, but since a loop and an if statement are two different constructs, that does not qualify the if statement as nested. A nested element must be inner in addition the inner element must be of the same kind as the outer element for it to qualify as nested.
An example of this are nested if statements or nested loops. In the case of a nested if statement, a nested if statement is an inner if statement within another if statement. Same with a loop, a nested loop is an inner loop within another loop. This, I think is an acceptable description of a nested element in any language, whether that is an OO language, a markup language or even a procedural language.
Today me and a few team-mates were preparing for the Java OCP exam and discovered in Java the designers redefine this concept in relation to classes. In Java all classes inside other classes are nested classes but only non-static classes are considered inner. The non-static nested class requires an instance of the outer class in order to exist and the static nested only uses the outer class name as a package convenience, using the class name as part of it’s package path (namespace in C#). Syntactically they are both inner classes and both nested but functionally only the non-static nested is an inner class. In other words the non-static member is a direct child of the outer class. As you can imagine this causes a lot of confusion if you are trying to answer questions related to nested classes.
I’ve noticed many areas where the design terminology and actual design can improve in Java. This is one area where the terminology should be adjusted to give the two concepts a clearer meaning. Sure you may remember this difference today and maybe even a week from now, but will you remember it a year from now? If I said which class requires and instance of it’s outer class? a nested one or an inner one? Nothing intuitively tells you or reminds which of these provides that. That’s because in our mind inner and nested mean the same thing (in relation to a class within another class). The class is defined inside of another class therefore it is nested (syntactically anyway). The difference is that one class is static the other is not. If I said the possible answers were static nested or non-static nested then that may actually provide a clue, as long as you know the different between the two. The problem is that in the exam sometimes the word static is left out only reference is inner class or nested class. An inner class is a static nested class. So it is a subset of the nested class. So in my opinion having two different names for the same thing creates a lot of room for confusion.
So what prompted me to write this post? Not much, just an observation where the Java design/terminology is a little weak and unclear. There are many other areas, this is only one of the many that come to mind. My heartburn in all this is that I have to now juggle and remember the redefinition of inner and nested if I want to not miss a whole bunch of questions in the exam related to this concept. In my opinion some of the same basic design principles of conventions and patterns apply in other areas such as language design and who knows maybe they are intended to be universal.


Popular posts from this blog

Simple Example of Using Pipes with C#

Difference Between Adapter and Bridge Design Patterns

Remote Access to Your Home Desktop Using No-IP