1

I have found below example in one of the answers: Java inner class and static nested class

   public class Container {
        public class Item{
            Object data;
            public Container getContainer(){
                    return Container.this;
            }
            public Item(Object data) {
                    super();
                    this.data = data;
            }

        }

        public static Item create(Object data){
            // does not compile since no instance of Container is available
            return new Item(data);
        }
        public Item createSubItem(Object data){
            // compiles, since 'this' Container is available
            return new Item(data);
        }
    }

I want to know why we do something like this: i.e. To get the instance of container why we create the instance of inner class? What is the use of this approach? Which design pattern it is? The above approach is already being used in one of the maintainance project, and I still didnt get whats the use of it?

Community
  • 1
  • 1
Atul
  • 1,694
  • 4
  • 21
  • 30

3 Answers3

3

The main purpose of this construct is the management of data. That the inner class hands out references to the "container" is just an unimportant implementation detail.

The problem with abstract and abridged examples like yours is: You just transfer the "how" from the writer to the reader of the code. But the "why" is completely lost.

So you can just replace Container with FileSystem and Item with File and data with some more internal state to a file. Then you can see:

  • A filesystem can have one or more files.
  • A file is exactly in one filesystem.
  • The lifetime of the File cannot exceed the lifetime of the filesystem.
  • The implementation between File and Filesystem is tightly coupled - each one might call the other - even private methods.

The last point is IMO the most important one: You can offer the slim and safe public API to the real user while File and Filesystem can use dangerous private methods of each other. In case of a Filesystem you don't want to grant anyone else access to these dangerous methods.

These traits are common for some problems - hence they are used.

A.H.
  • 63,967
  • 15
  • 92
  • 126
1

I want to know why we do something like this: i.e. To get the instance of container why we create the instance of inner class?

That is not what is happening.

In fact, you cannot create the instance of the inner class Item unless you already have an instance of the outer class Container on which you can call the createSubItem method. Creating the inner class instance doesn't create a new instance of the outer class. Rather it creates it in the context of the existing instance ... the one that is "available" when you invoke the inner classes constructor.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • sorry, in my project methods are static, I forgot to modify the above code..but structure is same, its getting the instance of outer class from inner class.. – Atul Sep 15 '12 at 12:59
  • 1
    @Atul - if you want us to provide an answer for different code, then show us the code. I'm not going to attempt to explain code that I cannot see. – Stephen C Sep 15 '12 at 13:04
  • @Stephan - sorry for trouble. I will post my code later, currently I dont have it with me. – Atul Sep 15 '12 at 13:06
0

the method in question is defined as static so it can access only static members of the class and since the Item class is not declared as static inner class it can't be accessed from a static function.

I am not sure about this specific design pattern or why it is required but this can work:

public static Item create(Object data) {
    Container c = new Container();
    return c.new Item(data);
}

One of the places we have used such design is just for having additional Comparator classes.

Bharat Sinha
  • 13,973
  • 6
  • 39
  • 63
  • I wanted to know the use of the approach mentioned in my question as I already have got one maintainance project using the same and I am still wondering why anyone would do something like this? – Atul Sep 15 '12 at 12:47
  • if you notice then Item class is having the method getContainer to get the instance of Container... – Atul Sep 15 '12 at 12:50
  • sorry, in my project methods are static, I forgot to modify the above code..but structure is same, its getting the instance of outer class from inner class.. – Atul Sep 15 '12 at 12:58
  • If the inner class is defined as `static`; it simply represents that that object alone doesn't have any significance... It is used with the OuterClass only. – Bharat Sinha Sep 15 '12 at 13:01