Are inner classes commonly used in Java? They are bad"?

Are inner classes commonly used in Java? Are they the same as nested classes? Or were they replaced by Java with something better? I have a book on version 5, and it has an example using the inner class, but I thought I read that the inner classes were "bad."

I have no idea and was hoping for thoughts of him.

Thank.

+17
java inner-classes
Jun 22 '09 at 18:58
source share
8 answers

Often internal classes are used, and something very similar - anonymous classes - is practically irreplaceable, as they are closest to what Java closes. Therefore, if you cannot remember where you heard that inner classes are bad, try to forget about it!

+27
Jun 22 '09 at 19:02
source share

They are not "bad" as such.

They can be abused (for example, inner classes of inner classes). As soon as my inner class spans more than a few lines, I prefer to extract it to my class. This helps to read and test in some cases.

There is one that is not immediately obvious, and worth remembering. Any non- static inner class will have an implicit reference to the surrounding outer class (the implicit "this" reference). This is usually not a problem, but if you start serializing the inner class (say using XStream ), you will find that it can cause unexpected grief.

+15
Jun 22 '09 at 19:06
source share

I do not think they are evil or bad. They may not be widely used, but they have many uses, callbacks are one of them. A particular advantage is that they can extend from a different class than the outer class, so you can have multiple inheritance.

I would say that one of the problems with inner classes is that their syntax is somewhat ugly. This is what discourages some people. There are many at work here.

+3
Jun 22 '09 at 19:03
source share

A good example of an inner class is an iterator implementation for a given type of collection. Its class, which implements a public interface, but does not have an existing business, except in connection with another class. It allows you to simulate things that in C ++ you would have to do with a friend’s operator.

+2
Jun 22 '09 at 20:05
source share

Non-static inner classes can hide the performance issue. They have access to member fields in the encompassing class, but not directly, but through recipients that are created automatically. This will be slower than just copying the members of the enclosing class to the inner class.

Some other issues with non-stationary inner classes are described here.

+2
Jun 22 '09 at 20:51
source share

They are useful and can be very widely used. Although you should be careful about the abuse of functionality, they can no longer be subjected to violence than any other language functions.

+1
Jun 22 '09 at 19:08
source share

The main thing to remember is that you are going to trade flexibility for simplicity and cohesion, making these two classes more closely related. You might want classes to be hard-coded, but you are reluctant to transparently replace other classes with what is currently your embedded class without defining your class from an interface outside the containing class.

0
Mar 12 '14 at 17:49
source share

Consider the following example:

 public class OuterClass { private AnonymousInnerClass anonymousInnerClass = new AnonymousInnerClass() { @Override protected void printAboutme() { System.out.println("AnonymousInnerClass.printAboutMe........."); Class clazz = this.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { String message = Modifier.isPublic(field.getModifiers()) ? "public":(Modifier.isPrivate(field.getModifiers())?"private":"protected"); message = message + " " + field.getType().getSimpleName(); message = message + " " + field.getName(); System.out.println(message); } } }; public void displayAnonymousInnerClass() { anonymousInnerClass.printAboutme(); } public void displayStaticInnerClass() { NestedStaticClass staticInnerClass = new NestedStaticClass(); staticInnerClass.printAboutMe(); } public void displayInnerClass() { InnerClass innerClass = new InnerClass(); innerClass.printAboutMe(); } public void displayMethodInnerClass(){ class MethodInnerClass { private String sampleField = "Method Inner Class"; public void printAboutMe() { System.out.println("MethodInnerClass.printAboutMe........."); Class clazz = this.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { String message = Modifier.isPublic(field.getModifiers()) ? "public":(Modifier.isPrivate(field.getModifiers())?"private":"protected"); message = message + " " + field.getType().getSimpleName(); message = message + " " + field.getName(); System.out.println(message); } } } MethodInnerClass methodInnerClass = new MethodInnerClass(); methodInnerClass.printAboutMe(); } class InnerClass { private String sampleField = "Inner Class"; public void printAboutMe() { System.out.println("InnerClass.printAboutMe........."); Class clazz = this.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { String message = Modifier.isPublic(field.getModifiers()) ? "public":(Modifier.isPrivate(field.getModifiers())?"private":"protected"); message = message + " " + field.getType().getSimpleName(); message = message + " " + field.getName(); System.out.println(message); } } } abstract class AnonymousInnerClass { protected String sampleField = "Anonymous Inner Class"; protected abstract void printAboutme(); } static class NestedStaticClass { private String sampleField = "NestedStaticClass"; public void printAboutMe() { System.out.println("NestedStaticClass.printAboutMe........."); Class clazz = this.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { String message = Modifier.isPublic(field.getModifiers()) ? "public":(Modifier.isPrivate(field.getModifiers())?"private":"protected"); message = message + " " + field.getType().getSimpleName(); message = message + " " + field.getName(); System.out.println(message); } } } 

}

this example compares each type of non-static nested class with a static nested class. Now, if you run the Outer class mapping method for each nested class, you will see the output of each printstable () method of the nested class, which has some reflection code for printing all member variables of the nested classes.

You will see that for non-nested classes there is one additional member variable that is different from the declared variable string in the code, which is present only at run time.

for example, if we execute the following code for InnerClass .: -

 public class NestedClassesDemo { public static void main(String[] args) { OuterClass outerClass = new OuterClass(); outerClass.displayInnerClass(); } 

}

the output is as follows: -

 InnerClass.printAboutMe......... private String sampleField protected OuterClass this$0 

Note that there is a mystery this $ 0 member variable of the type of the enclosing class (Outer class).

Now you understand that inner classes maintain a reference to an outer class. The script of the image in which you pass the link of the inner class to another outside world, and then the link will never be released, in turn, OuterClass also refers, therefore, to the leak.

Thus, this makes using inner classes bad if not used prpperly.

There is no such case with static inner classes. Please run all display methods. Also, if there is any problem in the code, please indicate.

0
Feb 03 '16 at 2:03
source share



All Articles