One of the main reasons to define a class inside of another class is encapsulation.Why, one might ask, would it be needed to define the class itself inside another as opposed to simply having an instance of the class as a field of the class? I would say if you wanted to keep the nested class code separate from the outer class code yet able to work with all of the internals of the outer class (the inner class code can access fields and methods of the outer class even if
private, unless the inner class is static and the fields and methods of the outer class are not static, in which case you'd need an object reference to access it (because it wouldn't know which object's field or method to access otherwise).Creating an inner class allows you to use separate code to do internal things to an outer class without adding to the outer class. Imagine the following:
public class MyClass{ private int myInternalField; private HelperClass helper = new HelperClass(); //Defined externally public void someMethod(){ //Code here to use helper to do something to myInternalField }} public class HelperClass{ public void doSomethingWithMyInternalField(){ //Won't work as-is //You'd have to add a getter and setter to MyClass //and probably make this method take a MyClass object //as a parameter like //helper.doSomethingWithMyInternalField( this ) //^as called from someMethod in MyClass }} If you had private fields and wanted a helper class instance that was a field of your class to be able to work on your private fields, like your
HelperClass object helper to be able to do something to myInternalField inside one of its own methods, the above wouldn't work as is, but the below would:public class MyClass{ private class HelperClass{ public void doSomethingWithMyInternalField(){ //Now you can freely access MyClass's myInternalField } } private int myInternalField; private HelperClass helper = new HelperClass(); public void someMethod(){ helper.doSomethingWithMyInternalField(); }} I made the class
HelperClass above private so it can only be accessed inside MyClass. I made the method doSomethingWithMyInternalField() public so it could be accessed outside of HelperClass, in other words, from code within MyClass.If you wanted your inner class to be
public so classes outside of MyClass could have and use a HelperClass object you could do that, but every HelperClass object would have to be inside a MyClass object. In that case, imagining the above MyClass code as it is but with HelperClass being public instead of private, if you wanted to create a HelperClass object over in some other class, you could do it like this:public class SomeOtherClass{ public void doSomethingElse(){ MyClass outer = new MyClass(); MyClass.HelperClass inner = outer.new HelperClass(); }} Notice the syntax. You first create an instance of the outer class,
MyClass. Then, the type of the inner class as you declare the reference inner is MyClass.HelperClass, and you assign the reference by typing the reference of your outer object followed immediately by the dot operator followed immediately by the new keyword followed by a space followed by the inner class constructor.Next up: Inner Class Example
















- Follow Us on Twitter!
- "Join Us on Facebook!
- RSS
Contact