What's the purpose of define an Inner-Classes inside a method ? | SoloLearn: Learn to code for FREE!

+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 ?

7/29/2017 7:08:50 AM

Corey

2 Answers

New Answer

+2

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

+16

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!