"Private" nested classes

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Etienne Boucher (etienne_at_novat.qc.ca)
Date: 07/29/04


Date: Thu, 29 Jul 2004 00:29:28 -0400

Nested classes are usualy objects made to only live in their parent object
instance. In other words...

public class Outter
{
    public class Inner
    {
    }
}

Prevent being able to use "new Outter.Inner()", in Main for exemple, while
still being able to access the members of Inner.

This could be done with a public interface implemented in a private nested
class, or like this:

public class Outter
{
    private Inner i;

    public Outter()
    {
        new Inner(this);
    }

    public class Inner
    {
        private Outter o;

        public Inner(Outter o)
        {
            if (null != o.i)
                throw new Exception();
            o.i = this;
            this.o = o;
        }
    }
}

But that's not very clean. Any one can think of a better way?

Etienne Boucher



Relevant Pages

  • Re: "Private" nested classes
    ... > Nested classes are usualy objects made to only live in their parent object ... > public class Outter ... > still being able to access the members of Inner. ...
    (microsoft.public.dotnet.languages.csharp)
  • Simple way to restore object relationships after deserialization?
    ... I'm working with XML serialization of hierarchical objects such as the ... public class OuterClass ... public InnerClass Inner ...
    (microsoft.public.dotnet.languages.csharp)
  • Protected inner classes and inheritance
    ... a top-level class with a protected inner class. ... I have a second-level class ... in a different package that extends the top-level class. ... public class SecondLevel extends TopLevel ...
    (comp.lang.java.programmer)
  • Re: "Private" nested classes
    ... Wouldn't making the inner constructor private solved the problem though? ... "Etienne Boucher" wrote: ... > outside the outter class. ... > public class Outter ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Question about extending inner class
    ... Unless an inner class is defined as static then it can not be created ... outside of the context of an enclosing class instance. ... public class Outer{ ... public static void main{ ...
    (comp.lang.java.programmer)