When should I use / examples of nested classes?

Please repeat this question to include the languages ​​it applies to.

So my java book had a whole chapter on nested classes, but ended with a note saying that you should only use them when it comes to "modeling composition relationships and introducing the internals of the class you want to hide". So let's discuss when you want to use nested classes and some examples.

+2


source to share


3 answers


A nested / inner class is just a class that is only used in the context of another class that does not have its own class file. If associated with an instance, it can only be instantiated in the context of an instance of the parent class; it can see private data or only private static data if it is a static class.

The Java developer site has a nested class tutorial with one example: http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html

A few examples of use:

  • Hide specific implementation Interface:

(Thinking about a database session for a tool like Hibernate): Suppose you have a Session interface and a SessionFactory that returns a session instance. The concrete class SessionImpl that implements the session interface can be a custom factory class that knows how to construct and initialize it.



  • Providing information through implementation Interface:

In the Wicket web environment, each GUI component has an associated "model" whose job it is to pass data to the component. The interface looks something like this:

public interface IModel extends IDetachable {
 public Object getObject();
 public Object setObject();
}

      

Suppose you have custom logic to fetch data for a custom GUI component that you wrote. Since no other component receives data in the same way, you can use an anonymous class at the point where the IModel is provided to take care of finding the data. If you have another point in the same class where you need to reuse the IModel implementation, you can make it an inner class. Later, if you need the model elsewhere, you can convert it to a top-level class.

Typically, you use an inner class in a situation where you need a class definition, but that class is only used or makes sense only in the context of the parent class.

+3


source


The real life use I had with nested classes was in the global settings object.

The parent class was Singleton, with nested classes as settings categories.



  • Settings
    • File settings
    • Print settings
    • Etc.

There was no real point in creating the inner object as separate classes, since they would be useless to them outside of the scope of the customization classes.

+3


source


I use nested classes for encapsulating algorithms, which are usually executed as a method with a lot of arguments. I am using a class that has raw data and I am putting the algorithms in a separate file in a nested class (using the incomplete keyword). This way I can put properties for this algorithm and its (working) data is preserved after the algorithm completes. I know it can be easily done without nested classes, but it seems to be correct because the algorithm is specifically crafted for the parent class.

   public partial class Network
    {
            partial void initFDLF()
            {
                fdlf=new FDLF(this);
            }

        public FDLF fdlf;
        public class FDLF
        {
            internal bool changed=true;
            internal bool pvchange=true;
            public double epsilon = 0.001;
            public bool fdlfOk=false;
            public void init(){...}
            public void run(){...}
            ...

      

+2


source







All Articles