Why static inner class




















Using a static nested class rather than non-static one may save spaces in some cases. For example: implementing a Comparator inside a class, say Student. Then the static ensures that the Student class has only one Comparator, rather than instantiate a new one every time a new student instance is created.

Non-static nested class' objects have access to the enclosing scope. That access to the enclosing scope is maintained by holding an implicit reference of the enclosing scope object in the nested object. Nested class is a way to represent the intent that the nested class type represents a component of the parent class. It can't be used separately. More details here. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.

Learn more. Static nested class in Java, why? Ask Question. Asked 13 years ago. Active 1 year, 1 month ago. Viewed k times. But I thought there might be other reasons, maybe performance. What might it be? Improve this question. Peter Mortensen 29k 21 21 gold badges 97 97 silver badges bronze badges. David Turner David Turner 4, 5 5 gold badges 25 25 silver badges 26 26 bronze badges. Add a comment. Active Oldest Votes. The Sun page you link to has some key differences between the two: A nested class is a member of its enclosing class.

Improve this answer. Community Bot 1 1 1 silver badge. DavidS Thanks for the link! Yeah, I was wrong, reading my comment now I see that my rephrase was incorrect.

As you said: An inner class interacts with the instance members through an implicit reference to its enclosing class , and this points out another interesting property of non-static inner classes as well as anonymous inner classes or local classes defined inside a block : they all can't have a no-arg constructor cause the compiler will implicitly prepend the arg sequence of every constructor in order to pass a reference of an instance of the enclosing class.

Pretty simple. Python Pillow. Python Turtle. Verbal Ability. Interview Questions. Company Questions. Artificial Intelligence. Cloud Computing. Data Science. Angular 7. Machine Learning. Data Structures. Single Class: The class loader need not take care of a new class each time, we create an object of the outer class.

We just get new objects for the same class over and over. I'm not quite sure what you mean here. The class loader is only involved twice in total, once for each class when the class is loaded. In Javaland we seem to be a bit scared to create classes. I think it has to feel like a significant concept. It generally belongs in its own file. It needs significant boilerplate. It needs attention to its design. Flexible access rules help you by cutting down the boilerplate, letting you keep the related code physically closer.

This reduces your 'mind-distance' between the two classes. If you'd asked 'why a non-static public inner class? You can use them anytime it helps with code readability and avoiding DRY violations and boilerplate.

I don't have a ready example because it doesn't happen all that often in my experience, but it does happen. Static inner classes is still a good default approach, btw.

Non-static has an extra hidden field generated through which the inner class refers to the outer. It can be used to create a factory pattern. A factory pattern is often used when the created objects need additional constructor parameters to function, but are tedious to provide each time:. The same can be done here as well in the form of multiple inner classes:. Less parameter passing is needed from factory to constructed class, but readability may suffer a bit.

Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Why prefer non-static inner classes over static ones? Ask Question. Asked 7 years, 6 months ago. Active 2 years, 2 months ago. Viewed 59k times. Another difference between static and non-static nested class is that you can not access non-static members e. While the Inner class can access both static and non-static members of the Outer class.

Skip to content. Change Language. Related Articles. Table of Contents. Save Article. Improve Article. Like Article.



0コメント

  • 1000 / 1000