> Hi
>
[quoted text clipped - 25 lines]
> Thanks
> Bob
use
static{
}
bagarow@yahoo.com wrote On 05/25/06 16:45,:
> Hi
>
[quoted text clipped - 5 lines]
> System.out.println ("Cups");
> // Would like to create a array of cup objects here
Go right ahead; nothing's stopping you.
> }
> }
[quoted text clipped - 8 lines]
> The constructor for class Cups does not get executed when the static
> field Cups.cupCount is referenced in the main method print statement.
No, of course not. The job of a Cups constructor is
to initialize a newly-created Cups object. You never ask
for any Cups objects to be created, so no Cups constructor
ever executes, or ever has any reason to execute.
> If in the initialization of the class Cups, I wanted to use a non
> simple code fragment e.g. creating an array of objects with a for loop,
> is there a way to do that in the client code without calling new
> Cuppas2()?
Creating a new Cuppas2 object won't do anything about
Cups. The Cuppas2 constructor (yes, there is one, even
though you can't see it) calls the no-argument constructor
of its superclass (Object, in this case) and then performs
any Cuppas2-specific initialization. Since there isn't any
Cuppas2-specific initialization to perform, the constructor
just returns after the Object constructor returns to it. At
no point does this do anything about the completely unrelated
class Cups: it doesn't make any new Cups objects, and in fact
it doesn't even cause the Cups class itself to be loaded.
Perhaps (*perhaps*) what you're struggling toward is the
static initialization block, something like
class Cups {
static int cupCount = 10;
static Cups[] cups = new Cups[cupCount];
static {
for (int i = 0; i < cups.length; ++i)
cups[i] = new Cups();
}
Cups() {
... as before ...
}
}
... but I confess I'm not at all clear as to your intent.

Signature
Eric.Sosman@sun.com
bagarow@yahoo.com - 26 May 2006 01:11 GMT
> bagarow@yahoo.com wrote On 05/25/06 16:45,:
> > Hi
[quoted text clipped - 64 lines]
> --
> Eric.Sosman@sun.com
I had a typo in my original email. Instead of saying new Cuppas2(), I
should have said I wanted the initialization of class Cups to create a
pool of Cup objects without having to call new Cups() in the client
code but instead by the invocation of the Cups class via referencing
the static field Cups.cupCount . So the static block you all have
suggested in Cups does the trick.
Thanks much
Bob
bagarow@yahoo.com - 26 May 2006 01:11 GMT
> bagarow@yahoo.com wrote On 05/25/06 16:45,:
> > Hi
[quoted text clipped - 64 lines]
> --
> Eric.Sosman@sun.com
I had a typo in my original email. Instead of saying new Cuppas2(), I
should have said I wanted the initialization of class Cups to create a
pool of Cup objects without having to call new Cups() in the client
code but instead by the invocation of the Cups class via referencing
the static field Cups.cupCount . So the static block you all have
suggested in Cups does the trick.
Thanks much
Bob
Thomas Hawtin - 26 May 2006 10:06 GMT
> static Cups[] cups = new Cups[cupCount];
> static {
> for (int i = 0; i < cups.length; ++i)
> cups[i] = new Cups();
> }
IIRC, Effective Java suggests doing something like:
static Cups[] cups = createCups();
private static Cups[] createCups() {
Cups[] cups = new Cups[cupCount];
for (int i=0; i<cups.length; ++i) {
cups[i] = new Cups();
}
return cups;
}
I prefer the former, but at least be aware of the possibility of the latter.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
bagarow@yahoo.com ha escrito:
> Hi
>
[quoted text clipped - 25 lines]
> Thanks
> Bob