instantiating one class inside another, but outside of all methods?

Discussion in 'Java' started by jkj2000@gmail.com, Jan 8, 2009.

  1. Guest

    Hi everyone,

    Bit of a newbie question here I guess. I'm reading a code snippet
    that instantiates a class inside another, but outside the
    constructor. It looks like this:

    public class MyClass
    {
    private ProxyClass default =
    new ProxyClass()
    {
    public MyComponent proxy()
    { //return something here}
    }

    //...
    // other methods defined here for MyClass
    //..

    }

    The ProxyClass above is declared and instantiated at the same time,
    but instantiation takes place outside a constructor or any other
    method of MyComponent. (ProxyClass is defined as an interface.)

    I've always assumed the new keyword had to be used within a method,
    constructor or otherwise. Maybe it's different here because the code
    is creating an anonymous inner class --please correct me if I'm wrong.

    In any event, when does the instantiation take place? At some point
    after the MyClass default constructor runs? I'm mainly trying to get
    the sequence of events correct. Thanks very much....
     
    , Jan 8, 2009
    #1
    1. Advertising

  2. Guest

    Re: instantiating one class inside another, but outside of allmethods?

    On Jan 8, 5:35 pm, wrote:
    > Hi everyone,
    >
    > Bit of a newbie question here I guess.  I'm reading a code snippet
    > that instantiates a class inside another, but outside the
    > constructor.  It looks like this:
    >
    > public class MyClass
    > {
    >   private ProxyClass default =
    >               new ProxyClass()
    >                 {
    >                     public MyComponent proxy()
    >                     { //return something here}
    >                 }
    >
    >  //...
    >  // other methods defined here for MyClass
    >  //..
    >
    > }
    >
    > The ProxyClass above is declared and instantiated at the same time,
    > but instantiation takes place outside a constructor or any other
    > method of MyComponent.  (ProxyClass is defined as an interface.)
    >
    > I've always assumed the new keyword had to be used within a method,
    > constructor or otherwise.  Maybe it's different here because the code
    > is creating an anonymous inner class --please correct me if I'm wrong.
    >
    > In any event, when does the instantiation take place?  At some point
    > after the MyClass default constructor runs?  I'm mainly trying to get
    > the sequence of events correct.  Thanks very much....


    A new-expression that invokes a constructor that throws no checked
    exceptions (whew, that was a mouthful) can be used to initialize a
    member or a static member the same way a primitive literal can be
    used:

    public class CanHasInteger {
    private int x = 2;
    /* ... */
    }

    is legal, as is

    public class CreateMe {
    public CreateMe () { // Note: no thrown exceptions
    /* ... */
    }
    }

    public class CanHasCreation {
    private CreateMe instance = new CreateMe ();
    /* ... */
    }

    There's a second thing going on in your example, though, which is that
    the new-expression that initializes private ProxyClass default also
    introduces an anonymous subclass of ProxyClass. This doesn't change
    the rules, but it makes the situation *look* more complicated than it
    is.

    -o
     
    , Jan 8, 2009
    #2
    1. Advertising

  3. wrote:
    > I've always assumed the new keyword had to be used within a method,
    > constructor or otherwise. Maybe it's different here because the code
    > is creating an anonymous inner class --please correct me if I'm wrong.


    It's a class field initialization expression. Any expression can be used
    to set to a field, including new, etc. In fact, this ability is actually
    relatively common.

    > In any event, when does the instantiation take place? At some point
    > after the MyClass default constructor runs? I'm mainly trying to get
    > the sequence of events correct. Thanks very much....


    The values are initialized after the call to the superclass constructor
    but before the body of a constructor (excluding the superclass
    constructor call). They are initialized in the order the variables are
    declared.

    See JLS §12.5 for more information on timing:
    <http://java.sun.com/docs/books/jls/third_edition/html/execution.html#44670>

    And see JLS §8.3.2 for more information on variable initializers:
    <http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.3.2>

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Jan 8, 2009
    #3
  4. Guest

    Re: instantiating one class inside another, but outside of allmethods?

    Thanks, gentlemen, to both of you!
     
    , Jan 9, 2009
    #4
  5. Roedy Green Guest

    On Thu, 8 Jan 2009 14:35:46 -0800 (PST), wrote,
    quoted or indirectly quoted someone who said :

    >The ProxyClass above is declared and instantiated at the same time,
    >but instantiation takes place outside a constructor or any other
    >method of MyComponent. (ProxyClass is defined as an interface.)

    this is called an anonymous class. It is similar to an inner class.

    See http://mindprod.com/jgloss/nestedclass.html
    see http://mindprod.com/jgloss/anonymousclasses.html

    for background.

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    PM Steven Harper is fixated on the costs of implementing Kyoto, estimated as high as 1% of GDP.
    However, he refuses to consider the costs of not implementing Kyoto which the
    famous economist Nicholas Stern estimated at 5 to 20% of GDP
     
    Roedy Green, Jan 9, 2009
    #5
  6. Lew Guest

    Re: instantiating one class inside another, but outside of allmethods?

    On Jan 9, 3:28 pm, Roedy Green <>
    wrote:
    > On Thu, 8 Jan 2009 14:35:46 -0800 (PST), wrote,
    > quoted or indirectly quoted someone who said :
    >
    > >The ProxyClass above is declared and instantiated at the same time,
    > >but instantiation takes place outside a constructor or any other
    > >method of MyComponent.  (ProxyClass is defined as an interface.)

    >
    > this is called an anonymous class. It is similar to an inner class.


    More than similar - an anonymous class *is* an inner class.

    > Seehttp://mindprod.com/jgloss/nestedclass.html
    > seehttp://mindprod.com/jgloss/anonymousclasses.html
    >
    > for background.


    GDP.
    > However, he refuses to consider the costs of not implementing Kyoto which the
    > famous economist Nicholas Stern estimated at 5 to 20% of GDP
     
    Lew, Jan 9, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SaravanaKumar
    Replies:
    6
    Views:
    9,455
    Tony Morris
    Oct 19, 2004
  2. Steve Richter
    Replies:
    6
    Views:
    726
    Steve Richter
    May 3, 2007
  3. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    Decralation of class inside other class and definition outside this class

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 13, 2007, in forum: C++
    Replies:
    2
    Views:
    344
    Alf P. Steinbach
    Jul 13, 2007
  4. Jeremy
    Replies:
    6
    Views:
    1,302
    Jeremy
    Dec 2, 2010
  5. Kenneth McDonald
    Replies:
    5
    Views:
    344
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page