Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are inner classes commonly used in Java? Are they "bad"? [closed]

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

I have no idea and was hoping for thoughts on it.

Thank you.

like image 815
johnny Avatar asked Jun 22 '09 18:06

johnny


People also ask

Is it good to use inner class in Java?

Java inner class is also very useful for providing security for an important piece of code. For instance, if an inner class is declared as private, it is unavailable to other classes which means that an object of the inner class cannot be created in any other classes.

What are the main disadvantages of using Java inner classes?

Inner classes have their disadvantages. From a maintenance point of view, inexperienced Java developers may find the inner class difficult to understand. The use of inner classes will also increase the total number of classes in your code.

Are nested classes bad practice?

In practice, nested classes should be a very rare occurrence; they are an indicator that the containing class is already too complicated.

Is it good to use inner class?

Inner classes are a security mechanism in Java. We know a class cannot be associated with the access modifier private, but if we have the class as a member of other class, then the inner class can be made private. And this is also used to access the private members of a class.


1 Answers

Inner classes are frequently used, and something very similar - anonymous classes - are practically indispensable, as they are the closest thing Java has to closures. So if you can't remember where you heard that inner classes are bad, try to forget about it!

like image 183
Daniel Earwicker Avatar answered Sep 28 '22 22:09

Daniel Earwicker