What's the purpose of define an Inner-Classes inside a method ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

What's the purpose of define an Inner-Classes inside a method ?

What's the purpose of define an Inner-Classes inside a method ? Why and when to do this ?

29th Jul 2017, 7:08 AM
Corey
Corey - avatar
2 Answers
+ 3
It is done when you want to use your own class on something but do not want the user to use it directly or when it does make sense to put it in, I only used it once in C# with unity in a space invaders game. The inner class was a Missile class with informations about missiles (GameObject (a class of unity), speed and direction) I made it in the game class. I do not know if it was the right thing to do but I did not need this class elsewhere
29th Jul 2017, 7:28 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 18
Inner classes are a good way to subdivide complex tasks. If you find yourself with an overly complicated object, an inner class can help clarify the code. Even if the division of labor is still too situationally specific, the encapsulation the subprocess is still a good design practice. It keeps your object divisions cleaner, and lets you focus on a single level of abstraction at a time. If you end up being that inner class functionality elsewhere, it's easily transferred!
29th Jul 2017, 7:31 AM
Jim
Jim - avatar