-4

I have read multiple posts about how to instantiate one object of the type of the inner class after instantiating one from the outer class; for example: here and here.
However, non of them actually explain if you can have multiple instances of the inner class with a single outer class instance and, if so, what the use cases are.

For example, would it be beneficial to have a controller/handler class be an outer class and the class we want to handle the inner class? What are the pros and cons of this or similar approaches with not one but multiple instances of the inner classes over the pillars of OOP (using inheritance, abstraction, polymorphism, and encapsulation)?
I am specifically asking about multiple instances; all of this is covered for single objects of the inner classes here.

ps* all of the links are StackOverflow questions.

sam
  • 54
  • 10
  • 1
    The only real difference between static and non-static (inner) classes is that inner classes have direct access to the member of the containing class. In either case, you're free to instantiate as many instances of the class as you want. You just have to instantiate an inner class within the context if the outer class, – CryptoFool Nov 28 '22 at 00:31
  • note that I do understand nested classes are a form of encapsulation. in that question, by encapsulation, I was referring to other types of encapsulation. – sam Nov 28 '22 at 00:33
  • @CryptoFool what are the use cases of the multiple instances? – sam Nov 28 '22 at 00:34
  • @sam Imagine something like a linked list. The data is stored in something like a `Node` class. That could be an inner class and you create one instance per list element. – f1sh Nov 28 '22 at 00:44
  • Yeah, as @f1sh says, a linked list is a perfect example. The outer class defines the List as a whole, and the inner class defines the nodes. As A) you don't want to allow the Node class to be used outside of the containing class, and B) it makes no sense to create Nodes for which there is no associated List, an inner Node class makes good sense. – CryptoFool Nov 28 '22 at 00:47

2 Answers2

2

With both types of classes contained within another class, you're free to instantiate as many instances of the class as you want.

The only real difference between static and non-static (inner) classes is that inner classes have direct access to non-static members of a parent/containing class instance. You have to instantiate an inner class within the context of an instance of the containing class to allow for that association. Both types have access to static members of the containing class.

Said another way, inner classes contain a reference to the instance of the containing class that created them. They can therefore access members of the containing class that, being instance members, require a reference to an instance of that class.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
0

Can non-static nested classes be instantiated multiple times. if so what are the use cases?

(1) To the first question, the answer is yes. And has been stated they have access to the enclosing classes instance fields.

(2) To the second question, because of (1) above, they are perfect for such things as creating an interator for a home grown data structure. Here is an example.

  • Note that the inner class has it's own index so it won't be shared among instantiations.
  • And it also has access to the private array in the enclosing class.
  • The Iterable interface allows support for the enhanced for loop.
public class MyDataDemo {
   public static void main(String[] args) {
       MyDataStructure<Integer> mds = new MyDataStructure<>(new Integer[] {1,2,3,4,5});

       for(int val :mds) {
           System.out.println(val);
       }
       System.out.println();
       
       // or via an iterator instance.
       Iterator<Integer> iter = mds.iterator();
       while(iter.hasNext()) {
           System.out.println(iter.next());
       }
   }
}


class MyDataStructure<T> implements Iterable<T> {
    private T[] values;
    public MyDataStructure(T[] values) {
        this.values = values;
    }
    
    public class MyIterator implements Iterator<T>{
        int index = 0;
        @Override
        public boolean hasNext() {
            return index < values.length;
        }
        
        @Override
        public T next() {
            return values[index++];
        }
    }
    
    
    @Override
    public Iterator<T> iterator() {
        return new MyIterator();
    }

}

prints

1
2
3
4
5

1
2
3
4
5
WJS
  • 36,363
  • 4
  • 24
  • 39