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

J

jkj2000

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

angrybaldguy

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
 
J

Joshua Cranmer

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>
 
R

Roedy Green

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top